【问题标题】:Call function when SwipeGestureRecognizer swipe speed passes threshold [closed]SwipeGestureRecognizer 滑动速度超过阈值时调用函数[关闭]
【发布时间】: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


【解决方案1】:
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 一起使用
请阅读您的问题下方的 cmets,并阅读 How to Ask 以了解您的下一个问题。这次你很幸运,我需要这个代码:)

【讨论】:

  • 谢谢,Xcode 说不是一切都好。我的代码现在看起来就像我在问题中编辑的一样
  • 对不起,'self' 应该是 'self.view!'
  • 好的,这解决了 Xcode 的问题,但现在,它没有检测到滑动。
  • 对这些值进行了反复试验,您是否将它们放在视图控制器中?
  • 还可以尝试在两个函数前面使用override 覆盖触摸函数
猜你喜欢
  • 1970-01-01
  • 2019-05-29
  • 1970-01-01
  • 1970-01-01
  • 2018-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多