0

我在 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
}

问题是当我点击登录/注册按钮时,它仍然会检测到点击手势,我实际上想要按钮触摸。

4

3 回答 3

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;  
}

希望有帮助

丹尼尔

于 2012-10-11T20:52:12.950 回答
0

如果您在该视图中添加按钮,则将在整个视图中添加您的添加手势识别器,而不是触摸按钮以创建在该视图中插入您的所有内容,然后仅为该视图添加手势识别器并为您的按钮创建另一个视图将工作。

于 2012-10-11T21:16:36.337 回答
0

解决此问题的方法是也为按钮设置手势并将代码作为

[tapToDismissKeyboard requireGestureRecognizerToFail:buttonTapGestureRecognizer];
于 2012-10-11T21:24:21.577 回答