苹果已经有摇一摇相应的API,直接调用就可以了.
@interface ViewController ()
@end
@implementation ViewController
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//成为第一响应者
[self becomeFirstResponder];
}
-(void)viewDidAppear:(BOOL)animated{
//失去第一响应者
[self resignFirstResponder];
[super viewDidAppear:animated];
}
- (void)viewDidLoad {
[super viewDidLoad];
}
-(BOOL)canBecomeFirstResponder{
return YES;
}
#pragma mark--开始摇一摇
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
if (motion==UIEventSubtypeMotionShake) {
NSLog(@"start");
}
}
#pragma mark--结束摇一摇
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
if (motion==UIEventSubtypeMotionShake) {
NSLog(@"end");
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
摇一摇就这两段代码,很简单是吧!