简体   繁体   English

将滚动事件传递给uiviewview中的uiimageview,并将其添加为要查看的子视图

[英]Passing touch events to uiimageview within a scrollview added as subview to view

I have added one scrollview and imageview to my view. 我在视图中添加了一个scrollview和imageview。 When I touch on this imageview my scrollview scrolls. 当我触摸此图像视图时,我的滚动视图将滚动。 And within my scrollview there are lots of imageviews which have touch events written separately. 在我的滚动视图中,有很多图像视图具有单独编写的触摸事件。

Here is the view hierarchy 这是视图层次结构

                          View
                            |
                            |
                    -------   --------
                   |                  |
                imageView1         scrollview
                                       |
                                  ...........................
                                  |           |       |.....|
                                 imageviews2

Imageview1 is above imagesview2 in appearance. Imageview1在外观上高于imagesview2。

And when I scroll the scrollview it is not possible to touch some of the imageViews within the scrollview as it comes behind the imageview within view. 而且,当我滚动滚动视图时,无法触摸滚动视图中的某些imageView,因为它位于视图中的imageview的后面。

How can I pass the touch events to the imageviews2 whenever it is behind imageview1. 每当触摸事件在imageview1后面时,如何将其传递给imageviews2。

You need to pass the touch event to the UIViews that are outside the bounds of the parent view. 您需要将touch事件传递给父视图范围之外的UIView。

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {

    // Convert the point to the target view's coordinate system.
    // The target view isn't necessarily the immediate subview
    CGPoint pointForTargetView = [self.targetView convertPoint:point fromView:self];

    if (CGRectContainsPoint(self.targetView.bounds, pointForTargetView)) {

        // The target view may have its view hierarchy,
        // so call its hitTest method to return the right hit-test view
        return [self.targetView hitTest:pointForTargetView withEvent:event];
    }

    return [super hitTest:point withEvent:event];
}

https://developer.apple.com/library/ios/qa/qa2013/qa1812.html https://developer.apple.com/library/ios/qa/qa2013/qa1812.html

You'll need to subclass UIView and override 您需要UIView并重写

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

in that subclass to skip imageView1. 在该子类中跳过imageView1。

You might get away with subclassing UIImageView and instantiating imageView1 as a member of that class, and overriding pointInside 您可以通过UIImageView并将其实例化为该类的成员来实例化imageView1,并覆盖pointInside

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{ return NO; }

不要忘记在滚动视图上取消选中“延迟内容触摸”和“可取消的内容触摸”!

I hope this answer might be useful for someone. 我希望这个答案可能对某人有用。 I have done it without using hittest. 我没有使用hittest就做到了。

Took all the subviews in scrollview and checked if it is an image view and did the following. 在scrollview中使用了所有子视图,并检查它是否为图像视图,并执行以下操作。

Passed the touchevents to another view using [view touchesBegan:touches withEvent:event]; 使用[view touchesBegan:touches withEvent:event]将touchevents传递到另一个视图; we have to write the same in touchesMoved and touchesEnded also. 我们还必须在touchesMoved和touchesEnded中编写相同的内容。

Thanks, 谢谢,

Joku 十余

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM