【发布时间】:2015-04-14 19:20:44
【问题描述】:
我目前正在编写一个应用程序,其中包含一个 UIView 和一个 UISwipeGestureRecognizer
我希望识别器能够识别用户向识别器方向拖动的速度。当速度足够高(超过特定阈值)时,应该发生自定义操作。和this post基本一样,但是我需要用Swift写的。
我如何将它翻译成 Swift 或者有更好的方法吗?
当前代码,标记为 cmets 的 Xcode 错误。
开始接触:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
//avoid multi-touch gesture
if(touches.count > 1){
return;
}
if let touch:UITouch = touches.first as? UITouch{
let locator:CGPoint = touch.locationInView(self.view!)
start = locator
startTime = touch.timestamp
}
触摸结束:
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
if let touch:UITouch = touches.first as? UITouch{
let locator:CGPoint = touch.locationInView(self.view!)
var dx:CGFloat = (locator.x - start!.x);
var dy:CGFloat = (locator.y - start!.y);
var magnitude:CGFloat = sqrt(dx*dx+dy*dy)
if (magnitude >= kMinDistance) {
// Determine time difference from start of the gesture
var dt:CGFloat = CGFloat(touch.timestamp - startTime!)
if (dt > kMinDuration) {
// Determine gesture speed in points/sec
var speed:CGFloat = magnitude / dt;
if (speed >= kMinSpeed && speed <= kMaxSpeed) {
// Swipe detected
swipedView()
}}}}}
【问题讨论】:
-
尝试翻译它,如果您有问题,请返回一个特定的问题
标签: ios swift uiswipegesturerecognizer swipe-gesture