Rectangle 27 0

objective c Continuous scrolling between UIPanGestureRecognizer and re enabled UIScrollView?


- (void) setContentOffset:(CGPoint)contentOffset
    {
        if(self.programaticScrollEnabled)
            [super setContentOffset:contentOffset];
    }
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
  • Declare the BOOL property and override setContentOffset: as described above.
  • In your setup code, set up a UIPanGestureRecognizer and set your state variable to allow programatic scrolling (assuming that's the default state you want): panRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)] autorelease]; //These properties may change according to your needs panRecognizer.cancelsTouchesInView = NO; panRecognizer.delaysTouchesBegan = NO; panRecognizer.delaysTouchesEnded = NO; [self addGestureRecognizer:panRecognizer]; panRecognizer.delegate = self; self.programaticScrollEnabled = YES;
  • Subclass UIScrollView (or subclass another subclass of UIScrollView, depending on your needs).
  • Turn programatic scrolling back on wherever you need it. For example: - (void)handleGesture:(UIPanGestureRecognizer *)gestureRecognizer { self.programaticScrollEnabled = YES; } - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { self.programaticScrollEnabled = YES; return YES; }

In any case, the key step is to NOT reuse scrollEnabled, but instead to subclass UIScrollView, create your own property to manage this state, and override setContentOffset:.

Manage which gestures can occur simultaneously. In my case:

The answer is a bit easier if you are only targeting iOS 5 and up, because in that case you really ought to reuse the UIScrollView panGestureRecognizer property.

Note