I have a Swift + SpriteKit app that loads an SKSpriteNode onto a scene, registers a UIPinchGestureRecognizer, and handles that pinch gesture with a simple handler function, like so:
func zoom(_ sender: UIPinchGestureRecognizer) {
// Don't let the map get too small or too big:
if map.frame.width >= 1408 && map.frame.width <= 3072 {
map.run(SKAction.scale(by: sender.scale, duration: 0))
}
print(map.frame.width)
}
However, a pinch will still bring the size of the sprite node to smaller than the specified limits and then when I try to un-pinch again the handler suddenly recognizes the limits I have placed and will not allow and un-pinch gesture.
I have tried doing the same thing with the recognizer's scale attribute:
func zoom(_ sender: UIPinchGestureRecognizer) {
// Don't let the map get too small or too big:
if sender.scale >= 0.9 && sender.scale <= 2.1 {
map.run(SKAction.scale(by: sender.scale, duration: 0))
}
print(map.frame.width)
}
but this is even weirder: the sprite node will stop getting smaller with a pinch, but then will grow tremendously massive with an un-pinch.
What is the correct way to put bounds on a pinch gesture?