如何在SpriteKit中限制缩放

4

我有一个使用Swift + SpriteKit开发的应用程序,它会将SKSpriteNode加载到场景中,并注册一个UIPinchGestureRecognizer手势识别器,在调用简单处理函数来处理这个捏合手势时,代码如下:

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)
}

然而,缩小手势仍会使精灵节点的大小小于指定的限制,然后当我再次尝试取消缩小手势时,处理程序突然识别到我设置的限制,并且不允许取消缩小手势。我已经尝试使用识别器的比例属性来做同样的事情:
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)
}

但更奇怪的是:精灵节点会在收缩时停止变小,但在放开手指后会变得异常庞大。

如何正确地限制捏合手势的范围?

1个回答

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;
}

这个项目中有一个不太好的地方,如果你快速捏合地图节点,则其大小可能会比if语句中规定的限制更小。如果你以正常速度进行捏合(不要太快),则会遵守if语句中规定的限制。

我不确定如何处理这种情况。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接