什么条件会导致UITapGestureRecognizer失败但触及开始成功?

时间:2014-06-10 22:43:37

标签: ios objective-c uiview uitapgesturerecognizer touchesbegan

请考虑以下代码:

@interface TouchDownGestureRecognizer : UIGestureRecognizer
@end

@implementation TouchDownGestureRecognizer
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesBegan");
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesMoved");
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesEnded");
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesCancelled");
}
@end

在从UIView

派生的类的构造函数中
- (id)initWithFrame:(CGRect)frame
{
    <snip>

    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    [tapRecognizer setDelegate:self];
    [self addGestureRecognizer:tapRecognizer];

    TouchDownGestureRecognizer *touchDownRecognizer = [[TouchDownGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouchDown:)];
    [self addGestureRecognizer:touchDownRecognizer];

    <snip>
}

此类的对象被添加到父视图中,并且在大多数情况下,点击子视图会导致调用touchesBegan,touchesEnded和handleTap。在某些情况下(我无法确定),handleTap会停止调用子视图,(而父调用的handleTap会被调用)。但是,即使handleTap停止调用,touchesBegan和touchEnded也会继续调用子视图。我确保UITapGestureRecognizer仍然在子视图的gestureRecognizers数组中。我还确保子视图的userInteractionEnabled属性为YES。是否存在UIView的一些已知条件或状态我们期望这种行为?

1 个答案:

答案 0 :(得分:0)

因此,如果我理解正确,在某些时候,子视图的父级的UITapGestureRecognizer正在调用它的“handleTap”选择器。当您在子视图的边界外点击时,可能会发生这种情况。 UIView的边界与它的框架不同,因为它的边界决定了UIView在确定内容绘制位置时接收触摸事件的位置。 UIView的界限与其框架无关,因此即使您在子视图的框架内,也可能触摸父级。

问题是,如果是这种情况,我认为子视图的touchesBegan和touchesEnded不会被调用,但如果你还没有检查过那么它就是一个可以启动的地方。也许GestureRecognizer仍然接收事件,因为它在视图的层次结构中(想想它冒泡)但由于它不负责该事件,它不会调用handleTap ......但这超出了我的范围和理解。