我有一个内部有一些视图的UIScrollView。当我点击它时,我希望能够理解触摸是发生在干净区域还是有子视图的区域。我怎么能这样做?
答案 0 :(得分:0)
我想我在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]));
}