- (void)viewDidLoad {
[super viewDidLoad];
// 设置允许摇一摇功能
[UIApplication sharedApplication].applicationSupportsShakeToEdit = YES;
// 并让自己成为第一相应者
[self becomeFirstResponder];
}
// MARK: - 摇一摇相关方法
// 摇一摇开始摇动
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
NSLog(@"开始摇动");
}
// 摇一摇取消摇动
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
}
// 摇一摇摇动结束
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.subtype == UIEventSubtypeMotionShake) { // 判断是否是摇动结束
// 添加震动 (<AudioToolbox/AudioToolbox.h>)
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
NSLog(@"摇动结束");
}
}