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

Ngo*_*yen 12 uitableview uigesturerecognizer ios

我有一个带有手势识别器的UITableView:

UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
[myTableView addGestureRecognizer:gestureRecognizer];
gestureRecognizer.cancelsTouchesInView = NO;
Run Code Online (Sandbox Code Playgroud)

...当点击tableview以解除键盘时,一切正常.我的问题是,当我点击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;
}
 }
Run Code Online (Sandbox Code Playgroud)

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

nor*_*man 24

以下是更一般的 - 它没有与您的具体观点相关联:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{   
    if ([touch.view isKindOfClass:[UITextField class]] || 
        [touch.view isKindOfClass:[UIButton class]]) 
    {
        return NO;
    }
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

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


小智 7

我遇到过同样的问题.我在我的视图中也实现了以下方法:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ((touch.view == self.textField) && (gestureRecognizer == self.tapGestureRecognizer))
    {
        return NO;
    }

    return YES;
}
Run Code Online (Sandbox Code Playgroud)

但它仍然没有奏效.所以我在方法中设置了一个中断,看到当我单击该字段时,touch.view将被设置为它但是当我点击清除按钮时它将作为UIButton*进入.在那一点上,很明显发生了什么以及如何解决它.以下解决了这个问题.

if((touch.view == self.textField || [self.textField.subviews containsObject:touch.view]) && (gestureRecognizer == self.tapGestureRecognizer))
{
     return NO;
}
Run Code Online (Sandbox Code Playgroud)