UITouch 事件转发到的所有子视图的列表
Posted
技术标签:
【中文标题】UITouch 事件转发到的所有子视图的列表【英文标题】:List of all the subviews a UITouch event is forwarded to 【发布时间】:2012-12-04 16:44:52 【问题描述】:我有一个 UIScrollView,里面有一些视图。当我点击它时,我希望能够了解触摸是发生在干净区域还是存在子视图的区域。我该怎么做?
【问题讨论】:
【参考方案1】:我想我在Find which child view was tapped when using UITapGestureRecognizer找到了一个可能的解决方案
所以,在注册 UITapGestureRecognizer 之后,使用:
// // Intercept all the taps inside the view
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(tapDetected:)];
tapRecognizer.numberOfTapsRequired = 1;
tapRecognizer.numberOfTouchesRequired = 1;
tapRecognizer.cancelsTouchesInView = NO;
[self addGestureRecognizer:tapRecognizer];
包含以下代码就足够了:
- (void)tapDetected:(UITapGestureRecognizer*)recognizer
UIView* view = recognizer.view;
CGPoint loc = [recognizer locationInView:view];
UIView* subview = [view hitTest:loc withEvent:nil];
NSLog(@"HIT! in %@",NSStringFromClass([subview class]));
【讨论】:
以上是关于UITouch 事件转发到的所有子视图的列表的主要内容,如果未能解决你的问题,请参考以下文章