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

发布于 2024-12-01 11:08:14 字数 1916 浏览 0 评论 0原文

我有一个 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;
}
 }

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

I have a UITableView with a gesture recognizer added:

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

... everything works fine when tapping on the tableview to dismiss the keyboard. My problem is, my hideKeyboard method also calls when tapping on the "clear" button on my UITextField. Very strange.

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

Any help would be appreciated, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

弱骨蛰伏 2024-12-08 11:08:14

以下是更一般的内容 - 它与您的特定视图无关:

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

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

The following is a little more general - it's not coupled to your specific views:

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

Also, don't forget to set the delegate for the gesture recognizer, and mark the class as implementing the UIGestureRecognizerDelegate protocol.

因为看清所以看轻 2024-12-08 11:08:14

我有同样的问题。我认为还实现了以下方法:

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

    return YES;
}

但还是不行。因此,我在方法中设置了一个中断,发现当我单击该字段时,touch.view 将被设置为它,但当我单击清除按钮时,它会作为 UIButton* 进入。那时发生了什么以及如何解决它是显而易见的。下面解决了这个问题。

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

I had the same issue. I also implemented the following method in my view:

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

    return YES;
}

But it still didn't work. So I set a break in the method and saw that when I click the field, touch.view would be set to it but when I clicked on the clear button it was coming in as a UIButton*. At that point it was obvious what was happening and what to do to fix it. The below resolves the issue.

if((touch.view == self.textField || [self.textField.subviews containsObject:touch.view]) && (gestureRecognizer == self.tapGestureRecognizer))
{
     return NO;
}
与风相奔跑 2024-12-08 11:08:14

我对 Ezmodius 方法的问题是,他依赖于一个名为“textField”的属性,而我的 UIViewController 是一个控制器,所有其他 UIViewController 都继承自该控制器,因此我需要实现一种更通用的方法:只要任何 UIViewController 中有一个文本字段,需要清除,我实现了以下内容(在相同的gestureRecognizer方法中:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isKindOfClass:[UITextField class]] ||
    ([touch.view isKindOfClass:[UIButton class]] && [touch.view.superview isKindOfClass:[UITextField class]]))
{
    return NO;
}

return YES;
}

所以基本上我检查它是文本字段还是超级视图是文本字段的按钮(在本例中,文本字段内的清除按钮)它对我来说就像一个魅力,我在我的 UIViewController 类中实现了这个,它适用于发生这种情况的每个页面。

My issue with Ezmodius' approach is that he depends on a property called 'textField' and my UIViewController is a controller from which all my other UIViewControllers inherit, so i needed to implement a more generic approach: whenever there was a textfield in any UIViewController that needed to be cleared, i implemented the following (inside the same gestureRecognizer method:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isKindOfClass:[UITextField class]] ||
    ([touch.view isKindOfClass:[UIButton class]] && [touch.view.superview isKindOfClass:[UITextField class]]))
{
    return NO;
}

return YES;
}

So basically im checking whether it is a textfield or a button whose superview is a textfield (in this case, the clear button inside the textfield) and it works like a charm for me. I implemented this in my base UIViewController class and it works for every page where this happens.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文