一个类似微信的摇一摇功能:
// 设置允许摇一摇功能
[UIApplication sharedApplication].applicationSupportsShakeToEdit = YES;
// 并让自己成为第一响应者
[self becomeFirstResponder];
// 摇一摇开始摇动
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
NSLog(@"开始摇动");
return;
}
// 摇一摇取消摇动
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
NSLog(@"取消摇动");
return;
}
// 摇一摇摇动结束
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.subtype == UIEventSubtypeMotionShake) { // 判断是否是摇动结束
NSLog(@"结束摇动");
}
}