【问题标题】:How to count time while phone is shaking in swift?手机快速抖动时如何计算时间?
【发布时间】:2019-05-24 09:24:34
【问题描述】:

手机快速抖动时如何计算时间? 想要获取时间测量手机晃动的时间。

var timer = Timer()
var timeLeft = 0

override func motionBegan(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
    if(motion == .motionShake){
        timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(onTimerFires), userInfo: nil, repeats: true)
    }
}
override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
    if(motion != .motionShake){
        print("MotionEnded")
        timer.invalidate()
    }
}

计时器无休止地工作。摇停时如何停止定时器...​

【问题讨论】:

    标签: ios swift timer motion


    【解决方案1】:

    使用start timeend time 之间的抖动来测量timeInterval

    var startDate: Date?
    
    override func motionBegan(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
        if(motion == .motionShake){
            self.startDate = Date()
        }
    }
    
    override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
        if(motion != .motionShake){
            if let startDate = self.startDate {
                let timeInterval = Date().timeIntervalSince(startDate)
                print(timeInterval)
            }
        }
    }
    

    在上面的代码中,我存储了 Date 实例,motion beginsmotion ends timeInterval 是使用 current datestored date 计算的。

    【讨论】:

    • UIEvent 有一个 timestamp 字段,您可以使用它并保存对 Date 的调用
    【解决方案2】:

    试试下面的代码:这里的 startDate 是开始摇动时的时间戳。

    override func motionBegan(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
            startDate = Date()
            print("Motion begins")
        }
    
        override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
            print("Motion ends")
            let endDate = Date()
            let difference = Calendar.current.dateComponents([.hour, .minute, .second, .nanosecond], from: startDate!, to: endDate)
            let formattedString = String(format: "%02ld %02ld %02ld %02ld", difference.hour!, difference.minute!, difference.second!, difference.nanosecond!)
            print(formattedString)
        }
    

    输出将以小时、分钟、秒和纳秒为单位打印时差。

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      在真机上测试:

        var startShaking = CFAbsoluteTimeGetCurrent()
      
        override func motionBegan(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
          if motion == .motionShake {
            startShaking = CFAbsoluteTimeGetCurrent()
          }
        }
      
        override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
          if motion == .motionShake {
            let shakingTime = CFAbsoluteTimeGetCurrent() - startShaking
            print("shaking for \(shakingTime) seconds")
          }
        }
      

      【讨论】:

      • 它进入第一个 "if" ,但 "else" 不起作用。
      • 请看更新,我在真机上测试过,似乎可以正常工作
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-12
      相关资源
      最近更新 更多