SwipeGestureRecognizer滑动速度超过阈值时的调用功能

时间:2015-04-14 19:20:45

标签: ios swift uiswipegesturerecognizer swipe-gesture

我正在编写一个应用程序,其中包含UIView UISwipeGestureRecognizer

我希望识别器能够识别用户在识别器方向上拖动的速度。当速度足够高(超过特定阈值)时,应该发生自定义操作。它与this post中的基本相同,但我需要用Swift编写。

如何将其转换为Swift或者有更好的方法吗?

当前代码,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()
    }}}}}

1 个答案:

答案 0 :(得分:2)

var start:CGPoint?
var startTime:NSTimeInterval?

var kMinDistance:CGFloat   = 25.0
var kMinDuration:CGFloat   = 0.1
var kMinSpeed:CGFloat      = 100.0
var kMaxSpeed:CGFloat      = 500.0

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 location:CGPoint = touch.locationInView(self.view!)
        start = location
        startTime = touch.timestamp
    }
}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
    if let touch:UITouch = touches.first as? UITouch{
        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) {
            // 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
                }
            }
        }
    }
}

诚实需要说他没有经过测试,但我需要一些这样的代码,所以这是在旅途中转换的。

编辑: 经过测试,似乎与Swift 1.2一起使用 请阅读问题下方的评论,并阅读How to Ask以了解下一个问题。你这幸运的是我需要这段代码:))