1.将application的applicationSupportsShakeToEdit属性改成YES
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; [self.window makeKeyAndVisible]; self.window.rootViewController = [[ViewController alloc]init]; self.window.backgroundColor = [UIColor whiteColor]; application.applicationSupportsShakeToEdit = YES; return YES;}
2.设置响应
- (BOOL)canBecomeFirstResponder{ return YES;}- (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self becomeFirstResponder];}- (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [self resignFirstResponder];}
3.实现功能
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{ NSLog(@"begin");}- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{ NSLog(@"end");}