UIGestureRecognizer发射两次?

Mat*_*hew 16 iphone

我已经在我的视图控制器的viewDidLoad上设置了一个UITapGestureRecognizer,但不知怎的,它只为一次点击两次激活选择器方法.

UITapGestureRecognizer *g = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openInMapsApp:)] autorelease];
[self.mapView addGestureRecognizer:g];
Run Code Online (Sandbox Code Playgroud)

我的方法:

-(void)openInMapsApp:(UIGestureRecognizer*)g {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
                                                    message:@"This will open this location in the Maps application. Continue?"
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"OK",nil];
[alertView show];
[alertView release];
}
Run Code Online (Sandbox Code Playgroud)

小智 19

手势识别器以不同的手势状态发送动作.所以这不是一个错误.解决方法是:

 UITapGestureRecognizer *g = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openInMapsApp:)] autorelease];
[self.mapView addGestureRecognizer:g];
Run Code Online (Sandbox Code Playgroud)
-(void)openInMapsApp:(UIGestureRecognizer*)g {
if(g.state != UIGestureRecognizerStateRecognized)
    return;
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
                                                    message:@"This will open this location in the Maps application. Continue?"
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"OK",nil];
[alertView show];
[alertView release];
}
Run Code Online (Sandbox Code Playgroud)


小智 6

我有一个双重UIAlertView上升.如上面的Nicolas Rostov所示,这对我有用.无论是UIGestureRecognizerStateEndedUIGestureRecognizerStateRecognized美国创建了一个新alertView时,[alertView show]该块被使用.随着// [alertView show]注释掉,它们仍然出现在控制台上,但只发生了一个动作.

-(void) handleTapGesture:(UIGestureRecognizer*)sender{
    if(sender.state != UIGestureRecognizerStateRecognized)
    return;
Run Code Online (Sandbox Code Playgroud)


lav*_*voy 2

我在视图中添加了一个计时器,用于检查以确保触摸至少在半秒前,如果太早则忽略第二次触摸。

但这只是一种解决方法。我仍然想解决真正的问题。