一個類似微信的搖一搖功能:
// 設置允許搖一搖功能
[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(@"結束搖動");
}
}