如果滑动的速度大于指定的标准,我正在尝试做某事(打印“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")
}
}
}
}
}