iOSシミュレータではハードウェアメニューのシェイクジェスチャーで試せます。
ViewControllerクラスはUIViewControllerクラスをスーパークラスにしていますが、そのUIViewControllerクラスのスーパークラスはUIResponderクラスです。したがって、ViewControllerクラスはmotionBegan:withEvent:メソッドをオーバーライドして利用できます。
iPhoneをシェイクするとmotionBegan:withEvent:メソッドが実行され、第一引数のmotionにモーションにUIEventSubtypeMotionShakeが入っています。
実装ファイルは次のとおりです。
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// 継承しているUIResponderクラスのメソッド
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//モーションがシェイクのときに実行する
if(motion == UIEventSubtypeMotionShake){
int num = arc4random()%6 + 1;
NSLog(@"num %d", num);
}
}
@end