在swift中触摸方法中的滑动手势

时间:2017-01-04 09:45:57

标签: ios iphone swift sprite-kit touch

所以我已经在我的游戏中实现了手势识别器以检测我的播放器的运动,但发现它们没有给我我想要的结果,所以我正在寻找在触摸方法和水龙头中进行滑动手势也在触摸方法。我已设法使触摸功能在触摸方法中工作,但我无法实现在触摸方法中滑动的功能,我似乎无法找到有关如何执行此操作的教程。我的下面的代码显示了我尝试实现此目的的触摸方法:

class GameScene: SKScene {

 var touchOrigin = CGPoint()
 var player = SKSpriteNode()


override func didMove(to view: SKView) {

    backgroundColor = .black

    player = SKSpriteNode(texture: nil, color: .orange, size: CGSize(width: 50, height: 50))
    player.position = CGPoint(x: 0, y: 0)
    addChild(player)

}

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

}

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

}

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

    for touch in touches {

        var currentTouchPosition = touch.location(in: self)
        touchOrigin = touch.location(in: self)

        if (Int(touchOrigin.x) > Int(currentTouchPosition.x)) {

            player.position.x -= 50

        } else if (Int(touchOrigin.x) < Int(currentTouchPosition.x)) {

            player.position.x += 50

        }

        if touch.tapCount == 1 {

            player.position.y += 50 //replace this with function :)

        } else if touch.tapCount >= 2 {

            player.position.y += 150 // change to run shield effect :)

        }
    }

}

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

}

如何让触摸方法识别某个方向的滑动手势?如果他们向一个方向滑动而不是将他们的手指从屏幕上移开并在一个动作中滑回原点,我该如何制作它然后被识别为水龙头呢?

1 个答案:

答案 0 :(得分:2)

以下是如何检测滑动手势的示例。

首先,定义实例变量以存储起始位置和时间

var start:(location:CGPoint, time:TimeInterval)?

并定义滑动手势的参数。相应地调整这些:

let minDistance:CGFloat = 25
let minSpeed:CGFloat = 1000
let maxSpeed:CGFloat = 6000

touchesBegan中,保存每个新触摸事件的位置/时间

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        start = (touch.location(in:self), touch.timestamp)
    }
}

touchesEnded中,通过比较手势的距离和速度来确定用户的手势是否是滑动。对角线滑动测试是可选的。

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    var swiped = false
    if let touch = touches.first, let startTime = self.start?.time,
            let startLocation = self.start?.location {
        let location = touch.location(in:self)
        let dx = location.x - startLocation.x
        let dy = location.y - startLocation.y
        let distance = sqrt(dx*dx+dy*dy)

        // Check if the user's finger moved a minimum distance
        if distance > minDistance {
            let deltaTime = CGFloat(touch.timestamp - startTime)
            let speed = distance / deltaTime

            // Check if the speed was consistent with a swipe
            if speed >= minSpeed && speed <= maxSpeed {

                // Determine the direction of the swipe
                let x = abs(dx/distance) > 0.4 ? Int(sign(Float(dx))) : 0
                let y = abs(dy/distance) > 0.4 ? Int(sign(Float(dy))) : 0

                swiped = true
                switch (x,y) {
                case (0,1):
                    print("swiped up")
                case (0,-1):
                    print("swiped down")
                case (-1,0):
                    print("swiped left")
                case (1,0):
                    print("swiped right")
                case (1,1):
                    print("swiped diag up-right")
                case (-1,-1):
                    print("swiped diag down-left")
                case (-1,1):
                    print("swiped diag up-left")
                case (1,-1):
                    print("swiped diag down-right")
                default:
                    swiped = false
                    break
                }
            }
        }
    }
    start = nil
    if !swiped {
        // Process non-swipes (taps, etc.)
        print("not a swipe")
    }
}