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

前端之家收集整理的这篇文章主要介绍了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协议.

原文链接:https://www.f2er.com/iOS/336693.html

猜你在找的iOS相关文章