摇一摇功能系统已经帮我们实现好了,只需要在控制器调用下面方法就可以了:
//开始摇动
- (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(@"结束摇动");
}
return;
}
在模拟器中运行时,可以通过 Hardware - Shake Gesture 进行测试。