使精灵向各个方向移动

时间:2018-12-03 14:58:45

标签: ios swift sprite-kit uigesturerecognizer uiswipegesturerecognizer

@objc func handleSwipe(gesture: UIGestureRecognizer) {
    if let gesture = gesture as? UISwipeGestureRecognizer {
        switch gesture.direction {
        case .up:
            ballPlayer.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 0))
            ballPlayer.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 110))
            print("Swiped up")
        case .down:
            ballPlayer.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 0))
            ballPlayer.physicsBody?.applyImpulse(CGVector(dx: 0, dy: -110))
            print("Swiped down")
        case .right:
            ballPlayer.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 0))
            ballPlayer.physicsBody?.applyImpulse(CGVector(dx: 110, dy: 0))
            print("Swiped right")
        case .left:
            ballPlayer.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 0))
            ballPlayer.physicsBody?.applyImpulse(CGVector(dx: -110, dy: 0))
            print("Swiped left")
        default:
            print("No such gesture")
        }
    }

}

我试图使我的精灵节点沿所有方向移动,包括对角线以及90度到45度之间的每个角度。这就是我所拥有的,现在找不到要做什么。有帮助吗?

1 个答案:

答案 0 :(得分:0)

我认为UISwipeGestureRecognizer不够灵活,无法满足您的需求。您可能需要使用UIPanGestureRecognizer

获取从触摸事件开始到结束的向量

This tutorial on UIGestureRecognizer应该具有您要查找的内容,尤其是有关减速的部分。