iPhone - 手势识别器互相争斗

时间:2011-03-25 21:36:37

标签: iphone ios

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

这样的东西
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不会将其识别为双击,而是会看到它作为两个动作,拖动方法将运行两次。

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

感谢

2 个答案:

答案 0 :(得分:9)

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

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

也许你可以尝试类似的东西。

答案 1 :(得分:1)

这是Vagrant在swift中的回答:

"test1",2,2,,0,"Sun,day",Jan
"be,st3",1,0,,0,"Sunday",Feb1
"nest",0,0,,0,"Sunday",Jul
"rest,5",,,,0,"Sunday",Aug

不要忘记实施委托协议并将手势的代表设置为自己(我最初忘记了这一点)。