首先在App's Delegate中设定applicationSupportsShakeToEdit属性:
最后在你的view控制器中添加motionEnded:
最后,经过我的测试这个方法不是很好,个人还是觉得用重力感应好一点,比较精准。但是在不用的应用场景也是可以的。
- (void)applicationDidFinishLaunching:(UIApplication *)application {
application.applicationSupportsShakeToEdit = YES;
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
然后在你的View控制器中添加/重载canBecomeFirstResponder, viewDidAppear:以及viewWillDisappear:
-(BOOL)canBecomeFirstResponder {
return YES;
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
[self resignFirstResponder];
[super viewWillDisappear:animated];
}
最后在你的view控制器中添加motionEnded:
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (motion == UIEventSubtypeMotionShake)
{
// your code
NSLog(@"end animations");
}
}
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
if (motion == UIEventSubtypeMotionShake)
{
// your code
NSLog(@"begin animations");
}
}
最后,经过我的测试这个方法不是很好,个人还是觉得用重力感应好一点,比较精准。但是在不用的应用场景也是可以的。