Lir*_*hen 8 shake ios appdelegate swift swift3
如何在Swift中的AppDelegate(整个应用程序)中检测到设备震动?
我找到了解释如何在视图控制器中执行此操作的答案,但希望在我的应用程序中执行此操作.
jk2*_*k2K 18
如果要全局检测摇动,UIWindow会实现可以接收摇动动作事件的UIResponder.您可以将以下代码段添加到AppDelegate
extension UIWindow {
open override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
if motion == .motionShake {
print("Device shaken")
}
}
}
Run Code Online (Sandbox Code Playgroud)
在您的AppDelegate:中添加以下代码段:
override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent?) {
if motion == .MotionShake {
print("Device shaken")
}
}
Run Code Online (Sandbox Code Playgroud)
Swift 3.0版本:
override func motionBegan(_ motion: UIEventSubtype, with event: UIEvent?) {
if motion == .motionShake {
print("Device shaken")
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4044 次 |
| 最近记录: |