我正在尝试检测 iOS6 中的震动,似乎每个示例都是在 2009 年左右为 iOS3 编写的,并且没有任何工作正常。这是我的方法:
- (void) motionEnded: (UIEventSubtype) motion withEvent: (UIEvent *) event {
if (motion == UIEventSubtypeMotionShake) NSLog(@"Detected a shake");
}
我已经尝试过 Apple 的建议(创建一个返回
canBecomeFirstResponder
的 YES
方法并调用它),但它什么也没做。它被称为罚款,但对无法识别的抖动没有影响。我已经阅读了一些关于需要创建我的 View 的自定义版本的内容,但我真的不想搞砸,因为我没有以编程方式创建 View ,并且有四个左右。
提前致谢!
最佳答案
在您的 viewController 中,您必须覆盖方法 caneBecomeFirstResponder 并回答是,在 viewDidAppear 中您必须将您的 viewController (self) 设置为第一响应者。这是代码:
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (motion == UIEventSubtypeMotionShake) NSLog(@"Detected a shake");
}
-(BOOL)canBecomeFirstResponder {
return YES;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
关于objective-c - 试图检测抖动,从不调用motionEnded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13703140/