Reputation: 67
I have a scene (SpriteKit) in my game (not the main scene) where I want to display a very large map. I'm trying to get that scene to scroll in all directions based on touch and be able to interact with landmarks I place. I'm not having much luck and am looking for some advice.
The closest I got was using a UIPanGestureRecognizer which I had to add as a UIView instead of an SKNode. While I was able to scroll around an image (it was actually the image that was scrolling). I didn't appear to have a way to add objects in such a way that I could interact with them.
I also tried an example of a camera that should have followed my swipes with a finger but that didn't work either. I'm stuck and I'd love to hear your suggestions!
Upvotes: 1
Views: 412
Reputation: 67
After many hours of searching and testing I was able to get working solutions using both a UIPanGestureRecognizer and a separate test by adding a camera view to the scene. The ability to move with the camera may be a better solution since I can scroll around on the map with my finger and click on SPSpriteNodes. Using the UIPan option just moved my view around and I could drag it off screen. Also it didn't appear I could interact with SKSpriteNodes attached to the view that was getting dragged around. I'll post the camera code below I'm using. Now I just need to figure out how to add constraints!
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch : UITouch = touches.first!
let positionInScene = touch.location(in: self)
let previousPosition = touch.previousLocation(in: self)
let translation = CGPoint(x: (positionInScene.x) - (previousPosition.x), y: (positionInScene.y) - (previousPosition.y))
panForTranslation(translation)
//print(boardCamera.position)
}
func panForTranslation(_ translation: CGPoint) {
let position = boardCamera.position
let aNewPosition = CGPoint(x: position.x - translation.x, y: position.y - translation.y)
boardCamera.position = aNewPosition
}
Upvotes: 1