Reputation: 563
I've been working on adding zooming functionality to my project via an SKCameraNode
and a pinch recognizer. My camera node is overlooking the mainMap
node that contains my game and I would like to have limited zoom capability.
In my function handler for pinches I have this code:
func zoomView(recognizer: UIPinchGestureRecognizer){
let currentScale = (mainScene.camera?.xScale)!
let futureScale = (currentScale * recognizer.scale)
if futureScale > 1.1 || futureScale < 0.5{
recognizer.scale = 1.0
return
}else{
mainScene.camera?.setScale((mainScene.camera?.xScale)! * recognizer.scale)
}
recognizer.scale = 1.0
print("currentScale: \(currentScale)")
print("recognizerScale: \(recognizer.scale)")
print("scaleFactor: \(futureScale)\n")
}
I've looked at many somewhat similar questions but they don't have this exact issue. When the scale approaches the bound values and becomes values such as 1.09812164306641
, I tend to have a stuttering effect on the zoom as it seems to not be able to get to the exact boundary value.
Does anyone have advice on how to avoid this behavior? Most helpful would be an explanation of how to get the zoom to behave more naturally where on reaching a limit it does the little bounce effect customary to IOS applications. Really any coherent explanation on how to use cameras with smooth zooming would be much appreciated!
Thank you
Upvotes: 3
Views: 707
Reputation: 714
I implemented the bounce effect during panning and zooming by subclassing the SKCameraNode and adding velocity, friction and edge attraction attributes.
This allows us to set the camera's velocity to move and zoom itself when it updates. When the camera moves outside of its designated bounds the edge attraction force is applied resulting in the bounce effect you are looking for.
In essence the camera moves past its min/max value and then is softly pulled back to the exceeded limit.
This also gives the bonus effect of the camera not stopping suddenly when user touches end, but drifts to a stop.
It was too hard to develop inside of our full game, so we made a demo project on GitHub where we could focus entirely on the camera functionality.
https://github.com/HumboldtCodeClub/SKCamera-Demo
Upvotes: 4