2016-10-24 42 views
1

好吧,目前在我的游戏中,我可以拖动,滑动并轻拂节点。但是,我希望用户只能轻扫/轻拂节点,并且我不希望用户能够拖动节点。我怎样才能做到这一点?如何在不拖动的情况下轻弹/滑动精灵?

这里是我当前的代码:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 

    for touch in touches { 
     let location = touch.location(in: self) 

     if ball.frame.contains(location) { 
      touchPoint = location 
      touching = true 
     } 
    } 
} 

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
    let touch = touches.first as UITouch! 
    let location = touch!.location(in: self) 
    touchPoint = location 

    for touch in touches { 
     let location = touch.location(in: self) 
     touchPoint = location 
     if touching { 
      if touchPoint != ball.position { 
       let dt:CGFloat = 1.0/60.0 
       let distance = CGVector(dx: touchPoint.x-ball.position.x, dy: touchPoint.y-ball.position.y) 
       let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt) 
       ball.physicsBody!.velocity=velocity 
      } 
     } 
    } 
} 

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    touching = false 

    for touch in touches { 
     let location = touch.location(in: self) 
     touchPoint = location 
     let node = self.atPoint(location) 
    } 
} 
+0

请定义更清楚你想达到什么。 – matthiaswitt

+0

我在猜测:你的目标是让节点不动,直到手指从滑动手势释放为止,此时手指在屏幕上的移动速度和距离决定了节点的移动速度和方向? – Confused

回答

1

这就是为什么UIGestureRecognizers存在。在您的视图中添加UIPanGestureRecognizer,并使用velocity(in:UIView)方法让您知道轻弹的方向和速度。

请注意,如果您只使用手势,则无法将手势放在节点上,只能查看视图,因此您需要将视图坐标转换为场景坐标。

您还可以让手势触摸到场景,以允许正常的触摸呼叫,并使用平移手势的速度。

https://developer.apple.com/reference/uikit/uipangesturerecognizer