简体   繁体   English

多级UIView(XIB类型)不触发UITapGestureRecognizer

[英]Multiple levels UIView (xib type) not trigger UITapGestureRecognizer

I want to dynamically load some UIView s, they're somewhat complex, so I spend few hours made .xib files. 我想动态加载一些UIView ,它们有些复杂,所以我花了几个小时制作.xib文件。

Now let's say I have a finalView , which as root/container view presented on screen. 现在假设我有一个finalView ,它作为根/容器视图显示在屏幕上。 Then I addSubView(subViewLvOne) , which also can be a container view. 然后,我也可以是容器视图的addSubView(subViewLvOne) At last subViewOne.addSubView(subViewLvTwo) , now we got everything need. 最后, subViewOne.addSubView(subViewLvTwo) ,现在我们有了所需的一切。

On screen it's perfect, exactly the result I want to show. 在屏幕上,它非常完美,正是我想要显示的结果。 Two inside One , One inside final . Two里面OneOne里面final

在此处输入图片说明

But I can not get any click event with One and Two . 但是我无法通过OneTwo获得任何click事件。 It seems final got all the events, but since many small subviews inside it, of course I want them to got their own event. 看来final获得了所有事件,但是由于其中包含许多小子视图,因此我当然希望他们获得自己的事件。

I've simplified my real situation, avoid many irrelevant details, hope now the description is clear. 我已经简化了我的实际情况,避免了许多无关的细节,希望现在的描述清楚。

Any suggestion is welcome, even like "use *** is better than .xib". 任何建议都是受欢迎的,即使“使用***优于.xib”也是如此。 I myself, already start doubting the direction of this whole solution. 我本人已经开始怀疑整个解决方案的方向。

After about 2 hours, I got two tips: 大约2个小时后,我得到了两个提示:

  1. the root UIView in .xib file, must be large enough. .xib文件中的根UIView必须足够大。

I have a UIView whose width is 200, and a UIButton inside it. 我有一个宽度为200的UIView ,并且其中有一个UIButton In runtime, I change the the width constraint of UIButton to fill the screen, obviously greater than 200. Then something ridiculous happens: only part of the button can trigger click event. 在运行时,我更改了UIButton的宽度约束以填充屏幕,显然大于200。然后发生了一件荒谬的事情:仅按钮的一部分可以触发click事件。 In this case, the width of root UIView , ie 200 pixel. 在这种情况下,根UIView的宽度(即200像素)。

  1. when init(frame:CGRect) , make width and height value large enough. init(frame:CGRect) ,使width和height值足够大。

At first, I copy CGRect.Zero from some examples online. 首先,我从网上的一些示例中复制了CGRect.Zero My colleague found this will block click event. 我的同事发现这将阻止点击事件。 You can just give a 1000-width and 1000-height, the outside area won't really trigger click event, meanwhile the UIView can be tapped correctly. 您可以只提供1000宽度和1000高度,外部区域不会真正触发点击事件,同时可以正确地点击UIView

That's all. 就这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM