iPhone - 手势识别器互相争斗 [英] iPhone - gesture recognizers fighting each other

查看:74
本文介绍了iPhone - 手势识别器互相争斗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在视图中添加了两个手势识别器。一个将处理视图拖动,另一个将处理双击。类似

I have added two gesture recognizers to a view. One will handle the view drag and the other the double tap. Something like

UITapGestureRecognizer *doubleTap =
  [[UITapGestureRecognizer alloc] initWithTarget:self 
        action:@selector(deleteThisView)];
[doubleTap setDelegate:self];
[doubleTap setCancelsTouchesInView:YES];
[doubleTap setNumberOfTapsRequired:1];
[base addGestureRecognizer:doubleTap];
[doubleTap release];

UIPanGestureRecognizer *panGesture = nil;
panGesture = [[UIPanGestureRecognizer alloc] 
                  initWithTarget:self action:@selector(drag:)];
[panGesture setMaximumNumberOfTouches:1];
[panGesture setDelegate:self];
[base addGestureRecognizer:panGesture];
[panGesture release];

问题在于:由于视图可以移动,因此双击很难获得,因为视图可以将一根头发滑向一侧或另一侧,iOS将不会将其识别为双击,而是将其视为两个动作,并且拖动方法将运行两次。

The problem is this: as the view can move, the double tap is somehow difficult to obtain, because the view can slide one hair to one side or the other and iOS will not recognize it as double tap, and instead will see it as two moves and the drag method will run twice.

我没有看到如何做到这一点。任何线索?

I am not seeing how this can be done. Any clues?

谢谢

推荐答案

有一个委托方法将会告诉中央手势代码两个识别器可能同时进行:

There's a delegate method that will tell the central gesture code that two recognizers might be going at the same time:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    if (gestureRecognizer==_panRecognizer && otherGestureRecognizer==_swipeRecognizer)
        return YES;
    if (gestureRecognizer==_swipeRecognizer && otherGestureRecognizer==_panRecognizer)
        return YES;
    return NO;
}

也许你可以试试这样的东西。

Maybe you can try something like that.

这篇关于iPhone - 手势识别器互相争斗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆