UITapGestureRecognizer 点击位置问题

发布于 2024-12-04 14:15:25 字数 402 浏览 4 评论 0原文

我有一个视图基本 UITextField 和“提交”按钮。当用户点击 UITextField 时,会出现键盘。我将视图向上移动,这样有用的位就不会被键盘阻挡,当键盘消失时,我会再次向下移动内容(注册键盘显示/隐藏通知)。

我希望用户能够点击视图中的任何位置来放弃键盘。所以我添加了一个 UITapGestureRecognizer 并在我的文本字段上调用 ​​resignFirstResponder 。效果很好......主要是。

如果您点击“提交”按钮,则不起作用。这不是cancelsTouchesInView,我将其设置为NO。这是移动的视图。 “点击”会在下移后视图中注册。因此,当我将视图向下滑动 80 点时,用户“在按钮上”进行的点击最终会位于按钮上方 80 点。

那么,如何在手势识别器事件触发并导致视图移动之前让点击通过呢?

I have a view basic UITextField and "submit" button. When the user taps in the UITextField the keyboard appears. I shift the view up so the useful bits aren't blocked by the keyboard and when they keyboard goes away I shift stuff back down again (registered for keyboard show/hide notifications).

I want users to be able to tap anywhere in the view to ditch the keyboard. So I added a UITapGestureRecognizer and I call resignFirstResponder on my text field. Works great.... mostly.

If you tap the "submit" button it doesn't work. It's not cancelsTouchesInView, I set that to NO. It's the moving view. The "tap" gets registered in the post shift down view. So when I slide the view down 80 points the tap the user made "on the button" winds up being 80 points above the button.

So how do I get that tap passed through before the Gesture Recognizer event fires and causes the view to shift?

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

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

发布评论

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

评论(3

羁绊已千年 2024-12-11 14:15:25

我找到了最适合我的修复方法。

UIGestureRecognizerDelegate 有一个方法-gestureRecognizer:shouldReceiveTouch:,它可以让你选择手势识别器是否应该接受触摸。

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

本质上,如果点击发生在 UIControl 对象的子类上,则让该对象处理点击,否则我的手势识别器会获取它。

I found a fix which works best for me.

UIGestureRecognizerDelegate has a method – gestureRecognizer:shouldReceiveTouch: which lets you choose say if the gesture recognizer should accept the touch.

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

In essence, if the tap happened on anything which is a sub-class of a UIControl object, let that object handle the click, otherwise my gesture recognizer gets it.

最佳男配角 2024-12-11 14:15:25

如果您希望视图像按钮一样使用 UIControl,它的作用类似于 UIButton 事件,并且是 UIView 的子类,因此您可以在用户点击视图并关闭键盘时添加 touchUpInside 事件,也可以在用户点击时关闭键盘提交按钮就可以了。希望这有帮助

If you want a view to act like a button use UIControl, it acts like UIButton events and is a subclass of UIView, so you can add a touchUpInside event when user taps the view and dismiss the keyboard, also dismiss the keyboard when the user taps the submit button and that should do the trick. Hope this helps

一世旳自豪 2024-12-11 14:15:25

我在许多应用程序中使用相同的功能。我没有使用 GestureRecognizer,而是将视图设置为 UIControl,而不是 UIView。您仍然可以执行使用 UIView 执行的操作,但您也可以指定在与视图交互时要执行的 IBAction。

操作方法如下: 在 Interface Builder 中,选择您的视图。然后,将其类分配给UIControl。 (目前它可能设置为 UIView。)

在该视图的 ViewController 中,编写一个 IBAction 方法来检测 backgroundTaps。我的看起来像这样:

- (IBAction)backgroundTap:(id)sender
{
    if ([textField1 isEditing]) {
        [textField1 resignFirstResponder];
    } else if ([textField2 isEditing]) {
        [textField2 resignFirstResponder];
    }
}

最后,在 Interface Builder 中,将您创建的 IBAction 连接到 UIControl

I use the same functionality in many of my apps. Rather than using the GestureRecognizer, I set my view up as a UIControl, rather than a UIView. You can still do the things you'd do with a UIView, but you can also assign IBActions to be performed when interacting with the view.

Here's how to do it: In Interface Builder, select your view. Then, assign its class to UIControl. (It's probably set up as UIView currently.)

In your ViewController for that view, write an IBAction method to detect backgroundTaps. Mine looks like this:

- (IBAction)backgroundTap:(id)sender
{
    if ([textField1 isEditing]) {
        [textField1 resignFirstResponder];
    } else if ([textField2 isEditing]) {
        [textField2 resignFirstResponder];
    }
}

Finally, in Interface Builder, connect the IBAction you created to the UIControl.

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