Reputation: 23
I want to know when the user stopped shaking and when he does, I would like to dismiss the alertViewController. Any help will be appreciated. Thanks.
I've tried both using methods motionEnded and motionBegan.
Here is my code. I want to dismiss the alertViewController when the user stops shaking the phone.
let alertController = UIAlertController(title: nil, message:
"Shaking", preferredStyle: UIAlertController.Style.alert)
override func motionBegan(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
if motion == .motionShake {
self.present(alertController, animated: true, completion: nil)
}
}
override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
if alertController.isBeingPresented {
alertController.dismiss(animated: true, completion: nil)
}
}
Upvotes: 1
Views: 249
Reputation: 4570
Use the following method for ended motion, See this post for more info https://developer.apple.com/documentation/uikit/uiresponder/1621090-motionended
declare alertController
outside of delegate method.
let alertController = UIAlertController(title: nil, message:
"Shaking", preferredStyle: UIAlertController.Style.alert)
override func motionBegan(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
if motion == .motionShake && !alertController.isBeingPresented {
self.present(alertController, animated: true, completion: nil)
}
}
override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
if motion == .motionShake {
alertController.dismiss(animated: true, completion: nil)
}
}
Upvotes: 1