【问题标题】:iOS Shake Gesture - Double ShakeiOS 摇一摇手势 - 双摇
【发布时间】:2017-06-14 02:45:38
【问题描述】:

我得到了正常的摇动手势,如下所示:

 import CoreMotion

 override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
      if motion == .motionShake {'
         print ("SHAKE GESTURE DETECTED")
      }
 }

我注意到有时这有点麻烦,例如,如果你突然放下设备,上面的代码就会调用。

有没有“双摇”之类的东西,摇两下即可调用?

【问题讨论】:

    标签: ios swift xcode core-motion shake


    【解决方案1】:

    没有官方的“双摇”手势,但您可以通过使用简单的计数器变量来实现:

    var shakeCount = 0
    override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
        if motion == .motionShake {
            shakeCount += 1
            if shakeCount == 2 {
                shakeCount = 0
                print ("SHAKE GESTURE DETECTED")
            }
        }
    }
    

    我还建议在第一次摇动后添加一个 NSTimer,它会在一定时间后重置摇动计数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-15
      • 2018-01-19
      • 2012-12-11
      • 2011-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-10
      相关资源
      最近更新 更多