How to limit scaling in SpriteKit

I have a Swift + SpriteKit application that loads SKSpriteNode onto a scene, registers a UIPinchGestureRecognizer and processes this gesture with a simple handler function, for example:

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, the pinch will still have the size of the node sprite smaller than the specified limits, and then, when I try to pinch again, the handler suddenly recognizes the limits that I have placed, and do not allow and do not allow, a creepy gesture.

I tried to do the same with the recognizer 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 node sprite will stop decreasing with a pinch, but then it will become extremely massive with the inconvenience.

What is the right way to put borders on a joke gesture?

+4
1

, , :

func zoom(_ sender: UIPinchGestureRecognizer) {

    // If the height of the map is already <= the screen height, abort pinch
    if (sender.scale < 1) {
        if (true) { print("pinch rec scale = \(sender.scale)") }
        if (map.frame.width <= 1408) {
            if (true) { print("Pinch aborted due to map height minimum.") }
            return
        }
    }

    // If the height of the map is already >= 2000 the screen height, abort zoom
    if (sender.scale > 1) {
        if (true) { print("pinch rec scale = \(sender.scale)") }
        if (map.frame.width >= 3072) {
            if (true) { print("Pinch aborted due to map height Max.") }
            return;
        }
    }

    map.run(SKAction.scale(by: sender.scale, duration: 0))
    sender.scale = 1;
}

, , , node , if. ( , ), , if.

, .

+1

Source: https://habr.com/ru/post/1667510/


All Articles