12

Swift の AppDelegate (アプリ全体) でデバイスの揺れを検出するにはどうすればよいですか?

ビューコントローラーでこれを行う方法を説明する回答を見つけましたが、アプリ全体でそうしようとしています。

4

2 に答える 2

8

に次のスニペットを追加しますAppDelegate

override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent?) {
    if motion == .MotionShake {
        print("Device shaken")
    }
}

スウィフト 3.0 バージョン:

override func motionBegan(_ motion: UIEventSubtype, with event: UIEvent?) {
    if motion == .motionShake {
        print("Device shaken")
    }
}

それ以降のバージョンでは、これはもう機能しないようです。代わりに、View Controller に上記のコードを追加する必要があります

于 2016-01-13T18:05:34.643 に答える