【发布时间】:2013-03-11 23:13:51
【问题描述】:
我已经在 iPad 和 iPad 模拟器上对此进行了测试,并且在一次成功摇动后,motionBegan 方法永远不会被调用。以下是我的程序的一个sn-p。
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if(event.type == UIEventSubtypeMotionShake)
{
NSLog(@"Shake event occurred.");
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Information" message:@"Some message here" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
alert.tag = shakeTag;
[alert show];
}
}
- (BOOL)canBecomeFirstResponder
{
return YES;
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:NO];
[self becomeFirstResponder];
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:NO];
}
-(void)viewDidDisappear:(BOOL)animated {
[self resignFirstResponder];
[super viewDidDisappear:NO];
}
究竟是什么阻止了motionBegan方法再次发生?我希望 UIAlertView 在第一次摇动时只显示一次,并在所有后续摇动时被关闭。我有一种预感,First Responder 仍然连接到 UIAlertView,这会阻止再次调用 motionBegan 方法。
更新:在我对应的 UIView 中,有一个 UIActionSheet 被创建。在我的 UIView 中调用并实现)并且我在 UIActionSheet 显示在屏幕上的同时触发了 motionBegan 方法(在我的 UIViewController 中),存在不再能够调用motionBegan 方法的问题。
之后,UIAlertView 从任何按钮选择中消失,不再调用 motionBegan 方法,但 UIActionSheet 工作正常。 UIView 中没有 firstResponder 分配,UIViewController 中只存在“canBecomeFirstResponder”。有什么想法吗?
【问题讨论】:
-
我复制并粘贴了您的代码,它在 iPad 模拟器上反复运行(我取出的唯一内容是 alert.tag = shakeTag 行)。
-
@rdelmar 它在 iPAD 模拟器上对我不起作用。硬件摇动手势不再调用motionBegan,因为以下行只有一个输出:“NSLog(@"Shake event occurred.");"我有以下方法来处理特定的警报标签,“- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex”
-
好吧,我不知道为什么你的不起作用。我添加了标签和警报委托方法,它对我来说仍然可以正常工作。
-
@rdelmar 这是在您的视图控制器类中吗?
-
是的。不是你的吗?
标签: ios objective-c cocoa-touch ios6 uialertview