Post

Replies

Boosts

Views

Activity

Trouble Zooming an SKView with SKCameraNode and UIScrollView
For the app I'm working on, I'm trying to leverage SpriteKit to do some render, but leverage UIKit and UIScrollView for scrolling phsyics that people expect from iOS. I've got things wired up such that the scroll view is layered on top of the SKView instance, and scrolling works as I expect. There's an SKCameraNode attached to the SpriteKit scene, and as a user scrolls the scroll view, it properly adjusts the camera node so that the content in my SpriteKit hierarchy is "scrolled" the same as the scroll view. However, when I zoom, I don't get the zooming behavior I expect. When pinching to zoom, the camera node properly adjusts its scale based off of the zoomScale of the scroll view, but it doesn't maintain the right origin for the zoom. I would expect that the zoom happens where the user is pinching, like it does with other zoomable content like a webpage in Safari, but I'm finding that the zooming causes my SpriteKit content to not match that. As best as I can tell from some other experiments with UIScrollView, the scroll view's contentOffset should properly be representing the pinch when zooming, so any calculations based off of that I would expect to zoom the content like I'm expecting. Scrolling does work correctly, no matter the zoom scale off the scroll view. I almost get the feeling that some value is being doubled or something and that's causing this, but I'm not sure where given my calculations. The gist of what I'm doing is listed below. class MapSceneView: UIView, UIScrollViewDelegate { private let scrollView: UIScrollView private let camera: SKCameraNode private func adjustCameraPosition() { let cameraPosition: CGPoint if let map { let visibleRect = CGRect( x: scrollView.contentOffset.x / scrollView.zoomScale, y: scrollView.contentOffset.y / scrollView.zoomScale, width: scrollView.visibleSize.width / scrollView.zoomScale, height: scrollView.visibleSize.height / scrollView.zoomScale ) cameraPosition = CGPoint( x: visibleRect.midX, // `map` is a value that corresponds to what my SpriteKit content is // displaying. `sizeInPixels` is intentionally in pixels, as we're // displaying image content that's always at a 1x scale. Subtracting // the height from the mid point should "flip" the coordinate system // to what's expected for SpriteKit. y: map.resolution.sizeInPixels.height - visibleRect.midY ) } else { cameraPosition = .zero } camera.setScale(1 / scrollView.zoomScale) camera.position = cameraPosition } func scrollViewDidScroll(_ scrollView: UIScrollView) { adjustCameraPosition() } }
0
0
34
1d
Trouble Zooming an SKView with SKCameraNode and UIScrollView
For the app I'm working on, I'm trying to leverage SpriteKit to do some render, but leverage UIKit and UIScrollView for scrolling phsyics that people expect from iOS. I've got things wired up such that the scroll view is layered on top of the SKView instance, and scrolling works as I expect. There's an SKCameraNode attached to the SpriteKit scene, and as a user scrolls the scroll view, it properly adjusts the camera node so that the content in my SpriteKit hierarchy is "scrolled" the same as the scroll view. However, when I zoom, I don't get the zooming behavior I expect. When pinching to zoom, the camera node properly adjusts its scale based off of the zoomScale of the scroll view, but it doesn't maintain the right origin for the zoom. I would expect that the zoom happens where the user is pinching, like it does with other zoomable content like a webpage in Safari, but I'm finding that the zooming causes my SpriteKit content to not match that. As best as I can tell from some other experiments with UIScrollView, the scroll view's contentOffset should properly be representing the pinch when zooming, so any calculations based off of that I would expect to zoom the content like I'm expecting. Scrolling does work correctly, no matter the zoom scale off the scroll view. I almost get the feeling that some value is being doubled or something and that's causing this, but I'm not sure where given my calculations. The gist of what I'm doing is listed below. class MapSceneView: UIView, UIScrollViewDelegate { private let scrollView: UIScrollView private let camera: SKCameraNode private func adjustCameraPosition() { let cameraPosition: CGPoint if let map { let visibleRect = CGRect( x: scrollView.contentOffset.x / scrollView.zoomScale, y: scrollView.contentOffset.y / scrollView.zoomScale, width: scrollView.visibleSize.width / scrollView.zoomScale, height: scrollView.visibleSize.height / scrollView.zoomScale ) cameraPosition = CGPoint( x: visibleRect.midX, // `map` is a value that corresponds to what my SpriteKit content is // displaying. `sizeInPixels` is intentionally in pixels, as we're // displaying image content that's always at a 1x scale. Subtracting // the height from the mid point should "flip" the coordinate system // to what's expected for SpriteKit. y: map.resolution.sizeInPixels.height - visibleRect.midY ) } else { cameraPosition = .zero } camera.setScale(1 / scrollView.zoomScale) camera.position = cameraPosition } func scrollViewDidScroll(_ scrollView: UIScrollView) { adjustCameraPosition() } }
Replies
0
Boosts
0
Views
34
Activity
1d