9

I can't seem to find anywhere how to implement a camera pinch to zoom in SpriteKit.

In my GameScene I can seem to run a zoom in action on the camera with:

let cameraNode = SKCameraNode()

cameraNode.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
addChild(cameraNode)
camera = cameraNode

let zoomInAction = SKAction.scale(to: 0.5, duration: 1)
cameraNode.run(zoomInAction)

But I can't seem to figure out how to translate this to a pinch to zoom feature

2
  • 1
    You need to add a gesture to your SKView to handle pinch, then translate it to the scene Commented Jun 2, 2017 at 20:17
  • did you still need help with this?
    – Fluidity
    Commented Jun 11, 2017 at 17:12

1 Answer 1

19

Here is a solution that worked for me, using gesture recognizers:

class GameScene: SKScene {

  var previousCameraScale = CGFloat()

  override func sceneDidLoad() {
    let pinchGesture = UIPinchGestureRecognizer()
    pinchGesture.addTarget(self, action: #selector(pinchGestureAction(_:)))
    view?.addGestureRecognizer(pinchGesture)
  }

  @objc func pinchGestureAction(_ sender: UIPinchGestureRecognizer) {
    guard let camera = self.camera else {
      return
    }
    if sender.state == .began {
      previousCameraScale = camera.xScale
    }
    camera.setScale(previousCameraScale * 1 / sender.scale)
  }

}

You can easily define a min and a max for the camera scale, and use your bounds on the calculous if needed.

3
  • This answer is buggy. After few iterations previousCameraScale will reach infinity.
    – kelin
    Commented Oct 28, 2021 at 10:47
  • I never had this problem, but as I said, I added boundaries to the camera scale, because I didn't want to allow an unlimited zoom. Also, as previousCameraScale is only set on the .began state, I don't see how it could reach infinity... If you find out, I'll be happy to know it.
    – Steve G.
    Commented Oct 29, 2021 at 11:19
  • 1
    I found out that you are right.
    – kelin
    Commented Oct 29, 2021 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.