【发布时间】:2016-04-18 19:14:21
【问题描述】:
如何在 Swift 的 AppDelegate 中(跨整个应用程序)检测设备抖动?
我找到了描述如何在视图控制器中执行此操作的答案,但希望在我的应用程序中执行此操作。
【问题讨论】:
标签: ios swift swift3 appdelegate shake
如何在 Swift 的 AppDelegate 中(跨整个应用程序)检测设备抖动?
我找到了描述如何在视图控制器中执行此操作的答案,但希望在我的应用程序中执行此操作。
【问题讨论】:
标签: ios swift swift3 appdelegate shake
在您的AppDelegate 中添加以下 sn-p:
override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent?) {
if motion == .MotionShake {
print("Device shaken")
}
}
Swift 3.0 版本:
override func motionBegan(_ motion: UIEventSubtype, with event: UIEvent?) {
if motion == .motionShake {
print("Device shaken")
}
}
【讨论】:
如果要全局检测摇晃动作,UIWindow 实现了可以接收摇晃动作事件的 UIResponder。可以在AppDelegate中添加如下sn -p
extension UIWindow {
open override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
if motion == .motionShake {
print("Device shaken")
}
}
}
【讨论】:
open。