gpt4 book ai didi

ios - 如何将 UIScrollView 的触摸事件发送到它后面的 View ?

转载 作者:技术小花猫 更新时间:2023-10-29 10:09:22 25 4
gpt4 key购买 nike

我在另一个 View 之上有一个透明的 UIScrollView。

ScrollView 有内容——文本和图像,用于显示信息。

它背后的 View 有一些用户应该能够点击的图像。并且它们上面的内容可以使用提到的 ScrollView 滚动。

我希望能够正常使用 ScrollView (虽然没有缩放),但是当 ScrollView 实际上没有滚动时,让点击事件传递到它后面的 View 。

结合使用触摸和滚动事件,我可以确定何时让点击通过。但它背后的 View 仍然没有收到它们。

我试过对所有触摸事件使用类似这样的东西:

- (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 只能识别 UIPanGestureRecognizerUIPinchGestureRecognizer 所以你需要添加一个 UITapGestureRecognizerUIScrollView,这样它也可以识别任何点击手势:

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: 来检测点击手势的位置是否确实在框架内 ScrollView 下方的其中一张图片,例如:

- (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;
}
}
}

这样,上面的代码只有在识别到点击手势时才会执行,如果点击位置落在图像内,您的程序只会对 ScrollView 上的点击使用react;否则,您可以照常滚动 UIScrollView

关于ios - 如何将 UIScrollView 的触摸事件发送到它后面的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27193136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com