Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS UIScrollView clipToBounds = NO Subviews not detected

I have a scroll view that is 256x256 in width/height. I have subviews of that scroll view stretching in both directions (left and right). I set clipToBounds to NO so I can see the items in the scroll view.

When the user touches a space in the scrollview holder (the area that extends beyond the 256x256) I check the hittest and return the scrollView.

My problem is the items on my scrollview are buttons, and if the button is not inside the 256x256 it is not receiving the touch events. How do I cycle through the buttons on the scrollview and then forward the events to the buttons?

I'm doing like this Paging UIScrollView with peeking neighbor pages

But my scrollview subviews aren't receiving events.

I've tried all of the following in my view that is supposed to send events down the wire and my buttons will not receive the events. The event chain always stops at my UIScroll view - allowing me to scroll my items - but how do I forward events to the buttons from the scrollview?

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if ([self pointInside:point withEvent:event]) {
        return self.scrollView;
    }
    return nil;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if ([self pointInside:point withEvent:event]) {
        return self.scrollView;
    }
    return nil;
}

- (UIView *) hitTest:(CGPoint) point withEvent:(UIEvent *)event {
    UIView* child = nil;

    if ((child = [super hitTest:point withEvent:event]) == self) {
        return self.scrollView;
    }
    return child;
}
like image 576
spentak Avatar asked Jan 16 '12 23:01

spentak


1 Answers

This was the answer and better solution. Instead of having the arbitrary view to forward events, we just do this in the UIScrollView and all the buttons and swipes work fine.

- (UIView )hitTest:(CGPoint)point withEvent:(UIEvent )event {
    UIView *view = [super hitTest:point withEvent:event];
    for (UIView *subview in self.subviews) {
        if (view != nil && view.userInteractionEnabled) {
            break;
        }
        CGPoint newPoint = [self convertPoint:point toView:subview];
        view = [subview hitTest:newPoint withEvent:event];
    }
    return view;
}
like image 199
spentak Avatar answered Nov 19 '22 13:11

spentak