1.在响应的controller中,设置canBecomeFirstResponder:
- (BOOL)canBecomeFirstResponder {
return YES;
}
2.实现下面的代理方法:
#pragma mark - 摇动
/**
* 摇动开始
*/
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (motion == UIEventSubtypeMotionShake) {
NSLog(@"开始摇了");
}
}
/**
* 摇动结束
*/
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
NSLog(@"摇动结束");
}
/**
* 摇动取消
*/
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
NSLog(@"摇动取消");
}