开发者

Disable touches on UIView background so that buttons on lower views are clickable

开发者 https://www.devze.com 2023-01-10 09:08 出处:网络
Basically, here is my view hierarchy (and I appologize if this is hard to read... I\'m new here so posting suggestions happily accepted)

Basically, here is my view hierarchy (and I appologize if this is hard to read... I'm new here so posting suggestions happily accepted)


--AppControls.xib

-------(UIView)ControlsView

----------------- (UIView)TopBar

----------------- -------------- btn1, btn2, btn3

----------------- UIView)BottomBar

----------------- --------------slider1 btn1, btn2

--PageContent.xib

----------------- (UIView)ContentView

----------------- --------------btn1, btn2, btn3

----------------- --------------(UIImageView)FullPageImage


My situation is that I want to hide and show the controls when tapping anywhere on the PageContent thats not a button and have the controls show, much like the iPhone Video Player. However, when the controls are shown I still want to be able to click the buttons on the PageContent.

I have all of this working, except for the last bit. When the controls are showing the background of the controls receives the touch events instead of the view below. And turning off user interaction on the ControlsView turns it off on all its children.

I have tried overriding HitTest on my Con开发者_运维百科trolsView subclass as follows which I found in a similar post:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
UIView *hitView = nil;
NSArray *subviews = [self subviews];
int subviewCount = [subviews count];
for (int subviewIndex = 0; !hitView && subviewIndex < subviewCount; subviewIndex++){
hitView = [[subviews objectAtIndex:subviewIndex] hitTest:point withEvent:event];
}   
return hitView;
}

However, at this point my slider doesn't work, nor do most of the other buttons, and really, things just start getting weird.

So my question is in short: How do I let all the subviews of a view have touch events, while the super view's background is unclickable, and the buttons on views below can receive touch events.

Thanks!


You're close. Don't override -hitTest:withEvent:. By the time that is called, the event dispatcher has already decided that your subtree of the hierarchy owns the event and won't look elsewhere. Instead, override -pointInside:withEvent:, which is called earlier in the event processing pipeline. It's how the system asks "hey view, does ANYONE in your hierarchy respond to an event at this point?". If you say NO, event processing continues below you in the visible stack.

Per the documentation, the default implementation just checks whether the point is in the bounds of the view at all.

Your strategy is to say "yes" when any of your subviews is at that coordinate, but say "no" when the touch would be hitting the background.

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    for (UIView * view in [self subviews]) {
        if (view.userInteractionEnabled && [view pointInside:[self convertPoint:point toView:view] withEvent:event]) {
            return YES;
        }
    }
    return NO;
}


Thanks to @Ben Zutto, Swift 3 solution:

override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
    for view in self.subviews {
        if view.isUserInteractionEnabled, view.point(inside: self.convert(point, to: view), with: event) {
            return true
        }
    }

    return false
}


Another approach may be to have an invisible full-screen button behind everything else, and take appropriate action when it is hit.


A slight variant on Ben's answer, dealing w/ children which extend outside their parent. If clipChildren is YES, then this will not return YES for points which are outside the main control but inside some child. if clipChildren is NO, this is the same as Ben's.

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    BOOL clipChildren = YES;

    if (!clipChildren || [super pointInside:point withEvent:event]) {
        for (UIView * view in [self subviews]) {
            if (view.userInteractionEnabled && [view pointInside:[self convertPoint:point toView:view] withEvent:event]) {
                return YES;
            }
        }
    }
    return NO;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消