Swift SpriteKit 3D Touch and touches moved

僤鯓⒐⒋嵵緔 提交于 2019-12-02 00:49:27
crashoverride777

After trying various other things that didnt work, including UISwipeGestureRecognizer subclasses, I finally figured it out.

The answer was actually staring me right in the face in that same stackOverflow post I linked in my question

iOS 9 Spritekit Double Tap Not Working on iPhone 6S

The solution is to create a property

var startingTouchLocation: CGPoint?

than in touches Began you set that property

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


        ....
     }
 }

and than add a check in touchesMoved to return from the method until the touch has actually moved.

 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 
       ....
     }
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!