iPhone:当用户触摸背景时关闭键盘

发布于 2024-11-08 07:30:04 字数 642 浏览 1 评论 0原文

我读过一些其他文章,例如此处此处但不幸的是我的代码中存在一些差异,无法使这些工作正常进行(所以请不要建议这些答案)。

我想当用户点击背景时关闭键盘。通常这很容易,除了我的 UITextField 对象位于 UIScrollView 内部,这使得我无法捕获触摸事件(UIScrollView > 吞掉它们,这样它们就不会到达基础视图)。解决此问题的一种方法是注册通用手势(点击),但这会捕获所有点击,包括用于提交按钮的点击。

所以基本上,“touchesBegan:withEvent:”不会工作,因为它永远不会被调用,手势也不会工作,因为它们不考虑按钮按下。

问题是:是否有某种方法可以检测 UIScrollView 上的简单点击?一旦我检测到水龙头,我就知道如何做剩下的事情。谢谢!

I have read some other articles like here and here but unfortunately there are some differences in my code that won't make these work (so please don't suggest these answers).

I would like to dismiss the keyboard when the user taps the background. Normally this would be easy, except that my UITextField objects are inside a UIScrollView which makes it so I can't catch the touch events (the UIScrollView swallows them so they don't reach the base view). One way to get around this is to register for a generic gesture (a tap), but this catches all taps, including the ones intended for the submit button.

So basically, 'touchesBegan:withEvent:' wont work because it never gets called, and gestures wont work because they dont account for button presses.

Here's the question: is there some way to detect a simple tap on a UIScrollView? Once I detect the tap I know how to do the rest. Thanks!

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

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

发布评论

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

评论(2

鱼窥荷 2024-11-15 07:30:04

您不能在滚动视图的超级视图上使用 touchesBegan:withEvent ,但是如何子类化 UIScrollView 并在那里处理触摸呢?然后,您可以正常调用 super 的实现,以避免踩到 UIScrollView 的脚趾:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    /* Insert code to dismiss keyboard if needed */

    // This makes sure scrolling proceeds normally.
    [super touchesBegain:touches withEvent:event];
}

You can't use touchesBegan:withEvent on the superview of the scrollview, but what about subclassing UIScrollView and handling the touch there? You can then proceed normally with a call to super's implementation to keep from stepping on the UIScrollView's toes:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    /* Insert code to dismiss keyboard if needed */

    // This makes sure scrolling proceeds normally.
    [super touchesBegain:touches withEvent:event];
}
追风人 2024-11-15 07:30:04

我也遇到了同样的问题。在 IOS 6.0 中,它工作正常,但是当我切换到 IOS 5.0 时,它开始显示您提到的相同行为。

IOS 5.0 的锻炼 - 您应该使用 UITapGestureRecognizer。然后将其委托设置为 self 并实现以下委托方法,

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return ! ([touch.view isKindOfClass:[UIControl class]]);
}
@end

上面的代码将验证触摸下的对象不是 UIButton 或任何控制元素,然后仅处理触摸

我希望它能解决您的问题。

I also ran into same problem. While in IOS 6.0, it was working fine, but as soon i switched to IOS 5.0, it started to show the same behaviour you have mentioned.

Workout for IOS 5.0 - You should use the UITapGestureRecognizer. Then set its delegate to self and implement the following delegate method

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;

as

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return ! ([touch.view isKindOfClass:[UIControl class]]);
}
@end

above code will verify that the object under touch is not a UIButton or any control element, then only handle the touch

I hope it would solve your problem.

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