考慮下面的代碼:什麼情況會導致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的狀態,我們期望這種行爲?
看起來是在http://stackoverflow.com/questions/19095165/should-superviews-gesture-cancel-subviews-gesture-in-ios-7相同的問題。 – Carl