我有几个IBActions附加到UIButtons。IBActions运行良好,直到我添加以下代码:
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"View Did Load");
[self addGestureRecognizersToView:drawImage];
}
在我添加了那块代码之后,IBActions不会启动。当我触摸UIButtons时,它们会高亮显示,但没有一个IBAction代码被击中。
这是我的addGestureRecognizers代码:
- (void)addGestureRecognizersToView:(UIImageView *)theView {
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[panGesture setMaximumNumberOfTouches:2];
[panGesture setMinimumNumberOfTouches:1];
//panGesture.delegate = drawImage;
[theView addGestureRecognizer:panGesture];
[panGesture release];
UITapGestureRecognizer *doubleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
[doubleFingerTap setNumberOfTapsRequired:2];
[self.view addGestureRecognizer:doubleFingerTap];
[doubleFingerTap release];
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[singleFingerTap setNumberOfTapsRequired:1];
[self.view addGestureRecognizer:singleFingerTap];
[singleFingerTap release];
}
如果我注释掉singleFingerTap代码,它就会工作。我想我不应该使用alloc,因为我以前已经在doubleFingerTap中分配过一次了?
你知道我在这里可能遗漏了什么吗?
单手指敲击会妨碍按钮的正常行为。你必须确保触球不受阻碍。
[singleFingerTap setCancelsTouchesInView:NO];
听起来UIMapGestureRecognizer正在拦截原本由UIButtons处理的抽头。
您可以使用gestureRecognizer:shouldReceiveTouch:
:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return (touch.view != self.button1 &&
touch.view != self.button2);
}
或者,你可以破解响应链。