【问题标题】:Swift SpriteKit 3D Touch and touches movedSwift SpriteKit 3D Touch 和触摸移动
【发布时间】:2016-03-17 12:20:56
【问题描述】:

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

控件如下

1) 屏幕左半边 = 跳转

2) 屏幕右半边 = 射击

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

现在在我的 touches started 方法中,我的拍摄有一点延迟,所以如果 touches 被调用,它会取消拍摄并开始慢动作。 这似乎是启用 3D Touch 的手机出现问题的原因。

深入研究我发现了这篇文章

iOS 9 Spritekit Double Tap Not Working on iPhone 6S

它基本上说明在启用 3D Touch 的设备上,touches move 方法会被自动调用。

  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. "

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

所以我的问题是

1) 能否在 SpriteKit 游戏中禁用 3D Touch?

2) 这是一个错误吗?

3) 在 3D Touch 设备上使用正确移动的触摸的任何其他方式?

4) 或者,我可以使用手势识别器来模拟我当前在触摸移动时所做的滑动和按住功能。

感谢您的帮助

【问题讨论】:

  • 只是一个快速的建议,看看 Demo Bots 示例应用程序,看看 iPhone 6S 上的该示例是否会出现同样的问题。
  • 感谢您的建议。我在我的项目中经常使用 DemoBots 示例。我刚刚检查过,他们使用了两次“touchesMoved..”方法。但是两者都在 SKSpriteNodes 的子类中,所以我想知道这是否有所不同。我没有 3d 触摸设备,所以测试起来很糟糕。
  • 我也没有。作为最后的手段,您可以通过 OTA 分发(例如 TestFlight)将演示机器人分发给您的朋友进行测试。
  • 是的,谢谢,我可以试试。列表中描述的方式似乎是一个错误。我现在正在玩替代品。再次感谢
  • 如果您有兴趣,我发布了一个关于如何解决这个问题的答案。再次感谢您的帮助

标签: swift touchesmoved 3dtouch


【解决方案1】:

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

在我在问题中链接的同一个 stackOverflow 帖子中,答案实际上正盯着我看

iOS 9 Spritekit Double Tap Not Working on iPhone 6S

解决办法是创建一个属性

var startingTouchLocation: CGPoint?

比在 touches 中开始设置该属性

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


        ....
     }
 }

然后在 touchesMoved 中添加一个检查以从该方法返回,直到触摸实际移动为止。

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

【讨论】:

  • 我遇到了同样的问题。在 iPhone 6S 上,一旦调用 touchesBegan,就会调用 touchesMoved 方法。我们需要注意这一点。
  • 是的,同样的问题,这解决了它! iPhone X 及更高版本运行良好,但在 8 及更低版本上触发 touchMoved 多次阻止我的逻辑。
猜你喜欢
  • 2016-11-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-30
  • 2014-09-16
  • 1970-01-01
  • 2016-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多