Passing touch info to UIScrollView under another UIView
Asked Answered
H

3

7

I have a UIScrollView under a transparant UIView. I need to transfer all pans and taps to the scroll view (which only scrolls horizontally). The regular UIView uses a subclass of UIPanGestureRecognizer to track vertical pans (Subclass found here). In the overlay view, I override the touch event methods to pass them to the UIScrollView.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self.scrollView.panGestureRecognizer touchesBegan:touches withEvent:event];
    [self.scrollView.singleTapGesture touchesBegan:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesCancelled:touches withEvent:event];
    [self.scrollView.panGestureRecognizer touchesCancelled:touches withEvent:event];
    [self.scrollView.singleTapGesture touchesCancelled:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    [self.scrollView.panGestureRecognizer touchesEnded:touches withEvent:event];
    [self.scrollView.singleTapGesture touchesEnded:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];
    [self.scrollView.panGestureRecognizer touchesMoved:touches withEvent:event];
    [self.scrollView.singleTapGesture touchesMoved:touches withEvent:event];
}

The overlay view, the vertical pan works perfectly. In the UIScrollView, the taps also work perfectly. The scrolling though, not so much. The scroll view scrolls about three points, then stops (as I continue with the horizontal pan.) If I let go with a velocity, the scroll view then picks up that velocity and finishes scrolling.

What would be the possible issues causing the scroll view to stop scrolling then pick up the velocity?

Heerlen answered 5/1, 2013 at 22:44 Comment(0)
D
2

From the code you posted, I assume you are not doing anything with the transparent UIView.. then why don't you just set userInteractionEnabled = NO; ??

Duenna answered 19/3, 2013 at 13:19 Comment(2)
Assume the transparent view needs to respond to at least one type of touch, such as a swipe or whatever.Reef
Uhmm... I do not know why your scrolling stops and pick up velocity.. but what I would've done is.. Have my UIScrollView handle all the events.. including the swipe that transparent UIView supposed to handle... and make a callback function that passes that event to that UIView... also disable the userInteractionEnabled on UIView.. so UIScrollView works the way it supposed to.. nd UIView will handle the only event it supposed to because UIScrollView will catch it and pass it to the UIView... maybe it is too late for you to go back and change it.. but sorry I do not know how to fix your problem.Duenna
A
0

Here's an easier solutions that worked well for me:

In the transparent UIView (let's call it OverlayView) make the width and height both 0 (so that the view is no longer technically on top of the UIScrollView) and set clipsToBounds = NO (so that the contents of OverlayView still show up on top of the UIScrollView).

 self.OverlayView.clipsToBounds = NO;
 CGRect frame = self.OverlayView.frame;
 self.OverlayView.frame = CGRectMake(frame.origin.x, frame.origin.y, 0, 0);

Note that if OverlayView contains interactive controls (like the button above) then they will no longer work. You'll need to move it into it's own view above the UIScrollView.

Anthodium answered 15/7, 2013 at 17:17 Comment(0)
E
0
#import "CustomUiView.h"

@implementation CustomUiView.h

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

The view you do not want to interact. create custom UIView class and use this custom view as your top view that return NO in pointInside method. Then the touch will go automatically underearth. In your case Your Transparent UIView will be CustomUiView subclass of UIView.

Early answered 12/11, 2017 at 9:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.