我在UIScrollView中有一些UITextField和UIButton,这个滚动视图位于我的UIViewController的视图中。我添加了一个触摸手势识别器来解除键盘,如果显示:
UITapGestureRecognizer *tapToDismissKeyboard = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
[tapToDismissKeyboard setCancelsTouchesInView:NO];
tapToDismissKeyboard.delegate = self;
[self.view addGestureRecognizer:tapToDismissKeyboard];
#pragma mark UITapGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isDescendantOfView:self.signupButton_] || [touch.view isDescendantOfView:self.profilePictureImageView_] || [touch.view isDescendantOfView:self.signupUsingFacebook_]) {
return NO; // ignore the touch
}
return YES; // handle the touch
}
问题在于,当我点击登录/注册按钮时,它仍会检测到轻击手势,我实际上想要触摸按钮。
答案 0 :(得分:0)
触摸的视图应该是按钮本身,所以这样做
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if (touch.view==self.signupButton_ ) {
return NO; // ignore the touch
}
return YES; // handle the touch
}
应该适合你,isDescendantOfView
返回false,因为该视图是你试图点击的按钮...同样如果你想让任何按钮通过你可以做
if([v isKindOfClass:[UIButton class]])
{
return NO;
}
希望有所帮助
丹尼尔
答案 1 :(得分:0)
如果您在该视图中使用了按钮,那么您将在整个视图中添加添加手势识别器,而不是按钮触摸该创建插入该视图中的所有内容,然后仅为该视图添加Gesture识别器并创建另一个视图你的按钮会起作用。
答案 2 :(得分:0)
解决此问题的方法是为按钮设置手势并将代码设置为
[tapToDismissKeyboard requireGestureRecognizerToFail:buttonTapGestureRecognizer];