2014-06-10 28 views
0

考虑下面的代码:什么情况会导致UITapGestureRecognizer失败,但触及开始成功?

@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> 
} 

这个类的对象被添加到父视图,派生的类和在大多数情况下,攻丝subview会导致touchesBegan,touchesEnded和handleTap被调用。在某些情况下(我一直无法查明),handleTap停止调用子视图,(并且父代的handleTap被调用)。然而,即使handleTap停止调用,touchesBegan和touchedEnded继续为子视图调用。我已确保UITapGestureRecognizer仍处于子视图的gestureRecognizers数组中。我还确保子视图的userInteractionEnabled属性为YES。是否有一些已知的条件或UIView的状态,我们期望这种行为?

+0

看起来是在http://stackoverflow.com/questions/19095165/should-superviews-gesture-cancel-subviews-gesture-in-ios-7相同的问题。 – Carl

回答

0

所以,如果我理解正确,在某些时候子视图的父母的UITapGestureRecognizer调用它的“handleTap”选择器。当你在子视图边界之外轻敲时,可能会发生这种情况。 UIView的边界与它的框架不同,它的边界决定了UIView接收触摸事件的位置,而框架决定了内容的绘制位置。 UIView的边界独立于其框架,因此即使您处于子视图的框架内,您也可能会触摸父项。

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

相关问题