【发布时间】:2019-10-09 18:23:01
【问题描述】:
我想知道用户什么时候停止摇晃,什么时候停止摇晃,我想关闭 alertViewController。任何帮助将不胜感激。谢谢。
我已经尝试过使用motionEnded和motionBegan方法。
这是我的代码。当用户停止摇晃手机时,我想关闭 alertViewController。
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)
}
}
【问题讨论】:
-
motionEnded应该可以完成这项工作。为什么没有成功? -
但是如何在
motionEnded中隐藏alertController? -
将
alertController设为属性,以便您可以在两种方法中访问它。 -
@Sweeper 你能写下代码吗?
-
AtulParmar 已经在他的回答中写下了我的意思。