ios – 为什么在我的textField清除按钮上调用UIGestureRecognizer?

我有一个添加了手势识别器的UITableView:
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
[myTableView addGestureRecognizer:gestureRecognizer];
gestureRecognizer.cancelstouchesInView = NO;

…点击桌面视图关闭键盘,一切都正常.我的问题是,当我的UITextField上点击“清除”按钮时,我的hideKeyboard方法也会调用.很奇怪.

commentTextField = [[UITextField alloc] initWithFrame:CGRectMake(5,5,310,35)];
commentTextField.contentVerticalAlignment =UIControlContentVerticalAlignmentCenter;
commentTextField.borderStyle = UITextBorderStyleRoundedRect;
commentTextField.textColor = [UIColor blackColor]; //text color
commentTextField.font = [UIFont fontWithName:@"Helvetica" size:14.0];  //font size
commentTextField.placeholder = @"Enter a comment...";  //place holder
commentTextField.autocorrectionType = UITextAutocorrectionTypeNo;   // no auto correction support
commentTextField.keyboardType = UIKeyboardTypeDefault;  // type of the keyboard
commentTextField.returnKeyType = UIReturnKeySend;  // type of the return key
commentTextField.clearButtonMode = UITextFieldviewmodeAlways;   // has a clear 'x' button to the right
commentTextField.delegate = self;
[commentTextField setHidden:NO];
[commentTextField setEnabled:YES];
[commentTextField setDelegate: self];

hide keyboard method:
 - (void) hideKeyboard{

if(keyboard){

   [commentTextField resignFirstResponder];

    [UIView animateWithDuration:.3 
                  delay:.0
                options:UIViewAnimationCurveEaseInOut 
             animations:^{ // start animation block                   

                 [myTableView setFrame:CGRectMake(0,myTableView.frame.origin.y + 216,myTableView.frame.size.width,myTableView.frame.size.height)];

             } 
             completion:^(BOOL finished){


             }];

    keyboard = 0;
}
 }

任何帮助将不胜感激,谢谢!

解决方法

以下是一些更通用的 – 它不适用于您的具体意见:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceivetouch:(UITouch *)touch
{   
    if ([touch.view isKindOfClass:[UITextField class]] || 
        [touch.view isKindOfClass:[UIButton class]]) 
    {
        return NO;
    }
    return YES;
}

此外,不要忘记为手势识别器设置代理,并将该类标记为实现UIGestureRecognizerDelegate协议.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...