如何检测具有大量控件的 uiview 上的点击?
我的问题是关于点击检测。
我有一个 uiviewcontroller,并且 uiview 上有一些控件(标签、按钮、tableview、imageview 等)。
当我点击 uibutton 时,我会显示一个小 uiview(200x150),如果用户点击smallview 中的 uibuttons,我会隐藏smallview 。
但如果用户点击背景,我无法隐藏 uiview。
我尝试了这段代码。
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//NSLog(@"Touches began.");
[self hideShareView];
}
如果我点击 uiviewcontrols 视图中的另一个按钮,它就不起作用。
我只想让我的 uiviewcontrol 的 uiview 首先做出反应。
我认为它与firstResponder有关,但我不知道如何首先设置它。
编辑:我希望它像 ipad 中的 uiPopover 一样工作。
My problem is about tap detection.
I have a uiviewcontroller and there are some controls on uiview (labels, buttons, tableview, imageview, etc..)
When I tap the uibutton I display a small uiview (200x150), if the user taps the uibuttons in smallview I hide the smallview.
But I can't hide the uiview if the user taps the background.
I tried this code..
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//NSLog(@"Touches began.");
[self hideShareView];
}
It doesn't work if I tap the another button in the uiviewcontrols view.
I just want my uiviewcontrol's uiview to react first.
I think its about firstResponder but I dont know how to set it first.
edit: i want it to work like a uiPopover in ipad.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信正确的方法是在显示“小视图”时添加一个新的透明 UIView。您应该将 UITapGestureRecognizer 添加到该 UIView,以便在检测到点击时触发所需的选择器。另外,您必须确保视图排列正确,小视图位于顶部,透明 UIView 位于正下方,视图层次结构的其余部分位于透明 UIView 下方。
最后,您应该确保在删除小视图的同时从视图层次结构中删除透明 UIView。
这有道理吗?
I believe that the correct approach is to add a new transparent UIView when you display your "smallview". You should add a UITapGestureRecognizer to that UIView, in order to trigger the desired selector when the tap is detected. Also, you must ensure that the views are arranged properly, with your smallview being the one at the top, the transparent UIView being immediately below and the rest of the view hierarchy below the transparent UIView.
Finally, you should ensure to remove the transparent UIView from your view hierarchy at the same time that you remove your smallview.
Does that make sense?
请看看这个问题(how-to-make- a-superview-intercept-button-touch-events),它看起来确实高度相关。
Please have a look at this question (how-to-make-a-superview-intercept-button-touch-events), it does seem highly related.
尝试
将
您的小视图
(即共享视图)置于前面
或将您的主视图发送到小视图后面。如果仍然不起作用&
您不希望主视图在打开小视图时执行任何操作
,然后尝试[; setUserInteractionEnabled:否];
,但是请确保
仅当您不希望主视图在打开小视图时执行任何操作时才可以执行此操作try your hands with
bringing
yoursmall view
(i.e. shareview)to front
or sent your main view behind your small view.If it still doesn't work &
you don't want your main view to perform any action when smallview is opened
then try[<YOUR_MAIN_VIEW> setUserInteractionEnabled:NO];
, butMAKE SURE
you can do this only when you don't want your main view to perform any action when smallview is opened