How to count time while phone is shaking in swift? want to get time measurement how long phone is shaking.
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()
}
}
The timer is working endlessly. How to stop the timer when shaking stop...