iPhone:当用户触摸背景时关闭键盘
我读过一些其他文章,例如此处和此处但不幸的是我的代码中存在一些差异,无法使这些工作正常进行(所以请不要建议这些答案)。
我想当用户点击背景时关闭键盘。通常这很容易,除了我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能在滚动视图的超级视图上使用
touchesBegan:withEvent
,但是如何子类化 UIScrollView 并在那里处理触摸呢?然后,您可以正常调用 super 的实现,以避免踩到 UIScrollView 的脚趾: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:我也遇到了同样的问题。在 IOS 6.0 中,它工作正常,但是当我切换到 IOS 5.0 时,它开始显示您提到的相同行为。
IOS 5.0 的锻炼 - 您应该使用 UITapGestureRecognizer。然后将其委托设置为 self 并实现以下委托方法,
如
上面的代码将验证触摸下的对象不是 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
as
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.