我在另一个视图之上有一个透明的 UIScrollView 。
滚动视图有内容 - 文本和图像,用于显示信息。
它后面的视图有一些用户应该能够点击的图像。
并且它们上面的内容可以使用提到的滚动视图进行滚动。
我希望能够正常使用滚动视图(尽管没有缩放),但是当该滚动视图实际上并未滚动以让点击事件传递到其后面的视图时。
使用触摸和滚动事件的组合,我可以确定何时让点击通过。
但其背后的视图仍然没有接收到它们。
我尝试过对所有触摸事件使用类似的东西:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touchesBegan %@", (_isScrolling ? @"YES" : @"NO"));
if(!_isScrolling)
{
NSLog(@"sending");
[self.viewBehind touchesBegan:touches withEvent:event];
}
}
但它不起作用。
同样就我而言,鉴于我拥有的用例,我无法真正应用 hitTest 和 pointInside 解决方案。
首先UIScrollView
s只是本能地认识到UIPanGestureRecognizer
s and UIPinchGestureRecognizer
所以你需要添加一个UITapGestureRecognizer
to the UIScrollView
因此它也可以识别任何点击手势:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
// To prevent the pan gesture of the UIScrollView from swallowing up the
// touch event
tap.cancelsTouchesInView = NO;
[scrollView addGestureRecognizer:tap];
然后,一旦您收到点击手势并且handleTap:
动作被触发,你可以使用locationInView:
检测点击手势的位置实际上是否在滚动视图下方的图像之一的框架内,例如:
- (void)handleTap:(UITapGestureRecognizer *)recognizer {
// First get the tap gesture recognizers's location in the entire
// view's window
CGPoint tapPoint = [recognizer locationInView:self.view];
// Then see if it falls within one of your below images' frames
for (UIImageView* image in relevantImages) {
// If the image's coordinate system isn't already equivalent to
// self.view, convert it so it has the same coordinate system
// as the tap.
CGRect imageFrameInSuperview = [image.superview convertRect:image toView:self.view]
// If the tap in fact lies inside the image bounds,
// perform the appropriate action.
if (CGRectContainsPoint(imageFrameInSuperview, tapPoint)) {
// Perhaps call a method here to react to the image tap
[self reactToImageTap:image];
break;
}
}
}
这样,上述代码仅在识别到点击手势时执行,如果点击位置落在图像内,则您的程序仅对滚动视图上的点击做出反应;否则,你可以滚动你的UIScrollView
照常。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)