如何将UIScrollView的触摸事件发送到它后面的视图? [英] How to send the touch events of UIScrollView to the view behind it?

查看:126
本文介绍了如何将UIScrollView的触摸事件发送到它后面的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在另一个视图的顶部有一个透明的UIScrollView。

i have a transparent UIScrollView on top of another view.

滚动视图包含内容 - 文本和图像,用于显示信息。

the scroll view has content - text and images, used to display info.

它背后的视图有一些用户应该能够点击的图像。
及其上的内容可以使用上面提到的滚动视图滚动。

the view behind it has some images that the user should be able to tap on. and the content over them is scrollable using the mentioned scrollview.

我希望能够正常使用滚动视图(尽管没有缩放),但是当滚动视图实际上并没有滚动以让点击事件通过它后面的视图。

i want to be able to normally use the scroll view (no zoom though), but when that scroll view is not actually scrolling to let the tap events through to the view behind it.

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

using a combination of the touch and scroll events i can determine when to let the taps through. but the view behind it still it does not receive them.

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

i have tried using something like this for all touch events:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    NSLog(@"touchesBegan %@", (_isScrolling ? @"YES" : @"NO"));

    if(!_isScrolling) 
    {
        NSLog(@"sending");
        [self.viewBehind touchesBegan:touches withEvent:event];
    }
}

但不起作用。

在我的情况下,根据我的用例,我无法真正应用hitTest和pointInside解决方案。

also in my case i cannot really apply the hitTest and pointInside solutions, given the use case that i have.

推荐答案

首先关闭 UIScrollView 只能固有识别 UIPanGestureRecognizer s和 UIPinchGestureRecognizer s所以你需要在 UIScrollView 中添加一个 UITapGestureRecognizer ,以便它可以识别任何攻丝手势:

First off UIScrollViews only inherently recognize UIPanGestureRecognizers and UIPinchGestureRecognizers so you need to add a UITapGestureRecognizer to the UIScrollView so it can recognize any tapping gestures as well:

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:来检测点按手势的位置是否实际位于滚动视图下方某个图像的帧内,例如:

Then once you receive that tap gesture and the handleTap: action is triggered, you can use locationInView: to detect whether the tap gesture's position is in fact within the frame of one of the images below your scroll view, for example:

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

This way, the above code is only performed if a tap gesture is recognized, your program only reacts to a tap on the scroll view if the tap location falls within an image; otherwise, you can just scroll your UIScrollView as usual.

这篇关于如何将UIScrollView的触摸事件发送到它后面的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆