以下用ios5实现摇动事件,关键代码如下(附件中有工程代码 ):
//该回调是运动事件开始 - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{ NSLog(@"motionBegan============="); if (motion == UIEventSubtypeMotionShake) { [[NSNotificationCenter defaultCenter] postNotificationName:@"shake" object:self]; } } //在运动事件结束时,第一响应者接收此回调 - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{ NSLog(@"motionEnded============="); } //与触摸一样,可以通过拨入手机和其他系统事件取消运动 - (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{ NSLog(@"motionCancelled=============="); } //绑定摇动后执行方法(该语句非常重要) //addShakeAnimations为需要执行的方法,在此末写出来,可根据实际应用来写 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addShakeAnimations) name:@"shake" object:nil];