UIView:让子视图接收触摸,但不接收主视图吗? [英] UIView: Let subviews receive touches, but not main view?
问题描述
我在此结构中有几个包含视图的单元格:
I've got several cells containing views in this structure:
- 主视图
- 后退
- frontview
单元格部分重叠.这意味着单元格A的主视图将部分覆盖单元格B的前视图.像这样:
The cells partly overlap. This means that the main view of cell A will partly cover the frontview of cell B. Like this:
- B主视图
- B后视
- B前视图
- 主视图
- 回顾
- 前视图
我想拦截前视图和后视图的触摸,但是我希望主视图忽略它们.
I want to intercept touches on frontviews and backviews, but I want main views to ignore them.
(我曾尝试在主视图上禁用用户交互,但这也会禁用前视图和后视图).
(I've tried disabling user interaction on main views, but that also disables front and back views).
有什么提示吗?
推荐答案
I found an answer here: http://vectorvector.tumblr.com/post/2130331861/ignore-touches-to-uiview-subclass-but-not-to-its
基本上,我将主视图作为UIView的子类,并使用以下方法覆盖hitTest:
Basically, I'm making the main view a subclass of UIView, and overriding hitTest with this:
-(id)hitTest:(CGPoint)point withEvent:(UIEvent *)event { id hitView = [super hitTest:point withEvent:event]; if (hitView == self) return nil; else return hitView; }
(请注意,对于有问题的UIView,您必须将UserInteractionEnabled设置为true,打勾,是的!)
(Note that confusingly you must set UserInteractionEnabled to true, ticked, yes for the UIView in question!)
这篇关于UIView:让子视图接收触摸,但不接收主视图吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!