我有一个 Swift SpriteKit应用程序,它将SKSpriteNode加载到场景中,注册UIPinchGestureRecognizer,并使用简单的处理函数处理该捏合手势,如下所示: func zoom(_ sender: UIPinchGestureRecognizer) { // Don't let th
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) }
但是,捏合仍会使精灵节点的大小小于指定的限制,然后当我尝试再次解除捏时,处理程序突然识别出我已经放置的限制,并且不允许和解除手势.
我尝试使用识别器的scale属性做同样的事情:
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) }
但这甚至更奇怪:精灵节点会因为捏合而停止变小,但是随着非压缩,精灵节点会变得非常庞大.
将限制放在捏合手势上的正确方法是什么?
这是我提出的解决方案,但有一点我不喜欢它: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语句中规定的限制将得到遵守.
我不知道如何处理这种情况.