LoginSignup
3
0

More than 5 years have passed since last update.

iPhoneのシェイクモーションを取得する

Posted at

iPhoneの動作検知について、少し勉強しています。
今回は、「iPhoneを振る」という動作の検知に関して備忘録を残します。

やること

シェイク動作の開始と終わりを検知する、のみです。
ライブラリのインストールなどは特に必要なく、下記のメソッドで取得できるみたいでした。

実装

// シェイクモーションの開始を検知
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {

    if (event.type == UIEventTypeMotion && event.subtype == UIEventSubtypeMotionShake) {

        NSLog(@"シェイクが開始されました。");

    }
}

// シェイクモーションの完了を検知
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {

    if (event.type == UIEventTypeMotion && event.subtype == UIEventSubtypeMotionShake) {

        NSLog(@"シェイクが終わりました。");

    }
}

// シェイクが開始されたのに、完了しなかった場合に呼ばれるようです。
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {

    if (event.type == UIEventTypeMotion && event.subtype == UIEventSubtypeMotionShake) {

        NSLog(@"シェイクがキャンセルされました。");

    }
}

デバック時

基本は実機で試せばいいと思いますが、シュミレーターでもデバックできるみたいです。
Hardware → ShekeGesture

まとめ

モーション系の実装はデバックが楽しかったりするので、他のものも試して行く予定です。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0