摇一摇示例代码。
iPhone上的摇一摇实现很简单。具体参考下面的代码或者下载Demo试一下。
- 在需要响应摇一摇的controller中,设置
canBecomeFirstResponder
:- (BOOL)canBecomeFirstResponder { return YES; }
- 直接实现下面的代理方法即可:
#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(@"摇动取消"); }
- 模拟器调试技巧:
- 发现问题请lssues我,谢谢:)
- Email:leoios@sina.com
- Blog: http://www.leodong.com
本项目采用 MIT license 开源,你可以利用采用该协议的代码做任何事情,只需要继续继承 MIT 协议即可。