计算滑动的速度

时间:2016-07-11 02:14:49

标签: ios swift swipe velocity

如果滑动的速度大于指定的标准,我正在尝试做某事(打印“Swiped”)。根据研究我已经提出了这个代码。每当我触摸屏幕时都会打印“开始”,但是当我第一次点击并按住屏幕时,“结束”和“滑动”仅打印,然后拖动,然后抬起(一个相当慢的过程)。我希望我可以轻弹屏幕并打印我的文字,但是现在它只会在刷卡过程中花时间打印。不是快速刷卡......你知道吗?希望这是有道理的!

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

print("Began")

    for touch in touches {
        let location:CGPoint = touch.locationInView(self.view!)
        start = location
        startTime = touch.timestamp
    }
}

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

    print("Ended")

    for touch in touches {
        let location:CGPoint = touch.locationInView(self.view!)
        var dx:CGFloat = location.x - start!.x;
        var dy:CGFloat = location.y - start!.y;
        var magnitude:CGFloat = sqrt(dx*dx+dy*dy)
        if (magnitude >= kMinDistance) {
            print("OK")
            var dt:CGFloat = CGFloat(touch.timestamp - startTime!)
            if (dt > kMinDuration) {
                var speed:CGFloat = magnitude / dt;
                if (speed >= kMinSpeed && speed <= kMaxSpeed) {
                    print("Swiped")
                }
            }
        }
    }
}

0 个答案:

没有答案