1
votes

I have been trying to figure out how to forward the touches from a UIScrollView to its superview. The reason is for sort of a cursor to follow the touch. Anyway, the scrollview is populated with UIButtons. My code for forwarding the touch is to use delegation in a scrollview subclass:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    if ([[self tfDelegate]respondsToSelector:@selector(tfDelegateBegan:)]) {
        [[self tfDelegate]tfDelegateBegan:[touches anyObject]];
    }
        NSLog(@"touches began");
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesMoved:touches withEvent:event];
    if ([[self tfDelegate]respondsToSelector:@selector(tfDelegateMoved:)]) {
        [[self tfDelegate]tfDelegateMoved:[touches anyObject]];
    }
        NSLog(@"touches moved");
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesEnded:touches withEvent:event];
    if ([[self tfDelegate]respondsToSelector:@selector(tfDelegateEnded:)]) {
        [[self tfDelegate]tfDelegateEnded:[touches anyObject]];
    }
        NSLog(@"touches ended");
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesCancelled:touches withEvent:event];
    if ([[self tfDelegate]respondsToSelector:@selector(tfDelegateCancelled:)]) {
        [[self tfDelegate]tfDelegateCancelled:[touches anyObject]];
    }
        NSLog(@"touches cancelled");
}

However, I have learned that UIScrollviews operate via UIGestureRecognizers, so these methods aren't even called by default. I realize that the gesture recognizers are exposed in iOS 5 but I need to support 4.0 as well. I did this instead:

       NSArray *a = [[theScrollview gestureRecognizers]retain];
        for (UIGestureRecognizer *rec in a) {
            if ([rec isKindOfClass:[UIPanGestureRecognizer class]]) {
                NSLog(@"this is the pan gesture");
                rec.cancelsTouchesInView = NO;
            }
        }

This allows the gesture to work and the touches methods to be called simultaneously. The issue is, now if you try to scroll while touching a button, the button can be pressed while scrolling. Normally, the scroll cancels the button and the only time a button can be pressed is if the scrollview is not scrolling. This is the desired functionality. Any suggestions on how I might achieve this?

1

1 Answers

1
votes

Maybe try controlling the button action using a flag that would prevent the event from firing if the scroll view is scrolling.

BOOL isScrolling = NO;

- (void) scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    isScrolling = YES;
}

- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    isScrolling = NO;
}

- (void) didTapButton {

    if(isScrolling)
        return;

    //Do Button Stuff
}