Question

I have UIView and I attached to it a UIPanGestureRecognizer. inside the UIView I have UIScrollView with paging enabled and I set the content size so scrollView could be scrolled just to the left.

the problem:

I want when user try to drag scrollView To the right, to send the event up to UIView so 'UIPanGestureRecognizer' can handle the touch event

Was it helpful?

Solution

at last, after 6 hours I figure it out I subclassed UIScrollView and implemented gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer delegate method

when user drag over the scrollView this method get called (BOOL)gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer and by default it returns NO

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    if (self.contentOffset.x > 0 &&  self.contentOffset.x <= self.contentSize.width - self.frame.size.width) {
        //user is in middle of dragging the scrollView don't allow any other gestureRecognizer to respond
        return NO;
    }else{
        //scrollView contentOffset is 0 and user is not dragging the scrollView so let other gestureRecognizer to respond
        return YES;
    }
}

OTHER TIPS

In trying to use homam's answer, I realized that the scrollView.panGestureRecognizer was being cut off after a return NO. What I wanted was for my custom pan recognizer to be cut off and the scrollView to still scroll. I think that might've been the result of the gestures being added at different levels, I'm not sure.

I found a solution, however, by changing my custom gesture recognizer to check for the scrollView.contentOffset and not activate unless it was greater than zero. I also turned off the scrollView.bounce property once the scrollView.contentOffset got to zero. When scrolling down on my scrollView it would hit the end and immediately my gesture recognizer would take over and move the superview out of the way like I wanted.

Here's a skeleton of my custom pan gesture

- (void)customPan:(UIPanGestureRecognizer*)panRecognizer
{
    if(panRecognizer.state == UIGestureRecognizerStateBegan)
    {
        //do begin stuff
    }

    if(panRecognizer.state == UIGestureRecognizerStateChanged)
    {
        if(self.scrollView.contentOffset.y > 0)
        {
            //do begin stuff to make sure it’s initialized properly when it does start to change
            self.scrollView.bounces = YES;
            return;
        }
        else
        {
            self.scrollView.bounces = NO;
        }

        //do change stuff
    }

    if(panRecognizer.state == UIGestureRecognizerStateEnded)
    {
        if(self.scrollView.contentOffset.y > 0)
        {
            //do begin stuff to make sure it’s initialized properly when it does start to change
            self.scrollView.bounces = YES;
            return;
        }
        else
        {
            self.scrollView.bounces = NO;
        }

        //do end stuff

    }
}

[myScrollView setContentSize:CGSizeZero];

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top