你可以做这样的事情......
首先...
在 App 的 Delegate 中设置 applicationSupportsShakeToEdit 属性:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
application.applicationSupportsShakeToEdit = YES;
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
第二...
在视图控制器中添加/覆盖 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];
}
第三...
将 motionEnded 方法添加到您的视图控制器:
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (motion == UIEventSubtypeMotionShake)
{
// your code
}
}
如果第一个答案没有,那应该可以工作,而且这只是快速输入而不是测试:)