iOS对于摇动进行了封装,直接可以使用MotionEvent来实现,代码如文档中实例,
首先设置view为firstResponder
- (BOOL)canBecomeFirstResponder { //默认是NO,所以得重写此方法,设成YES return YES; } - (void)viewDidAppear:(BOOL)animated { [self becomeFirstResponder]; } 然后实现下列方法://很像TouchEvent事件 - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { } - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { NSLog(@"shake"); } - (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event { }