考虑下面的代码:什么情况会导致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