iOS - forward all touches through a view

2019-01-04 08:52发布

I have a view overlayed on top of many other views. I am only using the overaly to detect some number of touches on the screen, but other than that I don't want the view to stop the behavior of other views underneath, which are scrollviews, etc. How can I forward all the touches through this overlay view? It is a subcalss of UIView.

9条回答
我想做一个坏孩纸
2楼-- · 2019-01-04 09:30

For passing touches from an overlay view to the views underneath, implement the following method in the UIView:

Objective-C:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    NSLog(@"Passing all touches to the next view (if any), in the view stack.");
    return NO;
}

Swift:

override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
    print("Passing all touches to the next view (if any), in the view stack.")
    return false
}
查看更多
劳资没心,怎么记你
3楼-- · 2019-01-04 09:32
myWebView.userInteractionEnabled = NO;

was all I needed!

查看更多
Juvenile、少年°
4楼-- · 2019-01-04 09:38

If the view you want to forward the touches to doesn't happen to be a subview / superview, you can set up a custom property in your UIView subclass like so:

@interface SomeViewSubclass : UIView {

    id forwardableTouchee;

}
@property (retain) id forwardableTouchee;

Make sure to synthesize it in your .m:

@synthesize forwardableTouchee;

And then include the following in any of your UIResponder methods such as:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [self.forwardableTouchee touchesBegan:touches withEvent:event];

}

Wherever you instantiate your UIView, set the forwardableTouchee property to whatever view you'd like the events to be forwarded to:

    SomeViewSubclass *view = [[[SomeViewSubclass alloc] initWithFrame:someRect] autorelease];
    view.forwardableTouchee = someOtherView;
查看更多
登录 后发表回答