2014-06-10 39 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 ...但這超出了我的範圍和理解。

相關問題