Swift SpriteKit 3D Touch and touches moved



我最近将我的游戏的一个版本发送到TestFlight进行内部测试。我的朋友注意到在他的iPhone 6S Plus上,拍摄控件无法正常工作。

控件如下

1)屏幕左半部分=跳转

2)屏幕右半部分=拍摄

3)滑动并按住屏幕的右半部分=开始慢动作

现在在我的触摸开始方法中,我的拍摄有轻微的延迟,所以如果触摸移动被称为它取消拍摄并开始慢动作。这似乎导致了支持3D Touch的手机上的问题。

进一步深入研究,我遇到了这篇文章

iOS 9 Spritekit 双击在 iPhone 6S 上不起作用

它基本上指出在启用 3D Touch 的设备上自动调用触摸移动方法。

  On 3D Touch capable devices, touch pressure changes cause     
   touchesMoved:withEvent: to be called for all apps running on iOS   
   9.0. When running iOS 9.1, the method is called only for apps  
   linked with the iOS 9.0 (or later) SDK.
    Apps should be prepared to receive touch move events with no   
    change in the x/y coordinates. "

这似乎可以理解为什么我的朋友有问题,如果总是调用触摸移动,即使他没有在屏幕上滑动,它也会取消拍摄并开始慢动作。

所以我的问题是

1)您可以在SpriteKit游戏中禁用3D Touch吗?

2)这是一个错误吗?

3) 还有其他方法可以使用在 3D Touch 设备上正确移动的触摸吗?

4) 或者,我可以使用手势识别器来模拟我当前在触摸移动中执行的滑动和保持功能。

感谢您的任何帮助

在尝试了其他各种不起作用的东西之后,包括UISwipeGestureRecognizer子类,我终于想通了。

答案实际上是在同一个堆栈中盯着我的脸我在问题中链接的溢出帖子

iOS 9 Spritekit 双击在 iPhone 6S 上不起作用

解决方案是创建一个属性

var startingTouchLocation: CGPoint?

比在触摸 开始你设置那个属性

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch in touches {
        let location = touch.locationInNode(self)
        /// Set starting touch. this is for 3d touch enabled devices, see touchesMoved method below
        startingTouchLocation = location

        ....
     }
 }

然后添加检查 触摸已移动 从方法返回,直到触摸实际移动。

 override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch in touches {
        let location = touch.locationInNode(self)
        /// WAY 1 - Check if touch moved because on 3d touch devices this method gets called on a force touch.
        guard location != startingTouchLocation else { return }
        // Your code
        ...
        /// WAY 2 - Add a min distance for the finger to travel in any direction before calling touches moved code
        let minValue: CGFloat = 30
        guard let startingLocation = startingLocation else { return }
        guard currentLocation.y < startingLocation.y - minValue ||
        currentLocation.y > startingLocation.y + minValue ||
        currentLocation.x < startingLocation.x - minValue ||
        currentLocation.x > startingLocation.x + minValue else { return  false }
       // Your code 
       ....
     }
  }

最新更新