iPhone: Click view behind transparent UIScrollView
Asked Answered
P

2

13

I have a UIScrollView that is set to have a clear background. Part of the scrollview does have content, but part does not (so it shows other views behind it). I would like to be able to click through the UIScrollView and to the MKMapView behind, but only for the transparent portion of the UIScrollView.

I have found some code which I am having a real hard time understanding how to get working:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if (![self yourMethodThatDeterminesInterestingTouches:touches withEvent:event])
        [self.nextResponder touchesBegan:touches withEvent:event]; 
}

Could someone help me wrap my mind around how to forward a touch event to a view that is behind another view? Can I call - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event from a UIViewController?

Pinnatifid answered 9/2, 2011 at 5:15 Comment(4)
funny, I was just having the opposite problem. my touches were being passed on for some reason. give me a minute and I'll see if I can remember what was happening.Kinna
ok, my situation isn't really relevant. what is it you don't understand? passing the touches on to the nextresponder seems reasonable.Kinna
I guess I have no idea anything about the nextresponder. How would I pass a touch onto this?Pinnatifid
That's what the code you posted does. You just have to write the method that determines whether or not it should be passed on.Spillage
C
9

What we did was to subclass UIScrollView and implement logic that passes responsibility down to views under it, if the touch happens inside of the transparent area.

In our case the transparent area is defined by contentOffset of 120 on Y axis, meaning our content starts 120 points below the start of the UIScrollView, and the code looks like this:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    if (self.contentOffset.y < 0 && point.y < 0.0) {
        return NO;
    } else {
        return YES;
    }
}

Obviously this response is well past its prime but hopefully this is helpful to anyone searching for a solution.

Cluj answered 15/3, 2013 at 18:8 Comment(0)
L
3

Basically, it's up to you to determine what touch events you care to forward to another responder. If you simply want to forward all touch events, just remove that if statement in the code you posted so the next responder will receive all the touch events.

Lox answered 18/2, 2011 at 23:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.