如何检测子视图中的触摸事件或当我们触摸子视图时如何使父视图被触摸?
我有 2 个 UIView。
第一个是父视图,
第二个是子视图,
我们如何检测子视图何时被触摸?
或者我想在用户触摸子视图时使父视图被触摸,任何代码可以帮助我做到这一点?可以这样做吗?
因为我有一个 Something Function,当其中一个被触摸时它就会调用。
I have 2 UIView.
the first one is a parent view
the second one is a subview,
how can we detect when a subview was touched?
or I want to make parent view was touched when user touch subview, any code can help me to do it? is it possible to do this?
because I have a Something Function, that will call when one of them was touched.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这对我有用:(
链接 xib 或 Storyboard 中的子视图)
ViewController.h
ViewController.m
This worked for me:
(Link the subview in xib or storyboard)
ViewController.h
ViewController.m
问题已经解决了。我认为有人给出了很好的答案,但我忘记了如何回答。
这就是我所做的。 XIB中有一个选项。有一个复选框(标题为“用户交互已启用”)指定子视图是否处理用户事件。
取消选中该复选框,对子视图的所有触摸都会转到父视图或其后面的任何其他视图。
The problem has been solved. I think someone gave a great answer but I forget how.
This is what I did. There is an option in XIB. There is a checkbox (titled "User interaction enabled") that specifies whether a subview handle user event or not.
Uncheck that checkboxes and all touch to the subview goes to the parent view or whatever other view behind it.
要检测触摸事件,您需要在子视图或超级视图(您想要触摸的位置)中添加
UITapGestureRecognizer
。然后您可以添加 UITapGestureRecognizer 的委托方法
希望它能给您带来想法。
to detect touch event you need to add
UITapGestureRecognizer
in your sub view or super view (in which you want get touch).then you can add delegate methods of
UITapGestureRecognizer
hope it gives you idea..
这可能有帮助:
This might help: