Scrolling is disabled when adding UIPanGestureRecognizer to UIScrollView
Asked Answered
M

4

8

I have a scroll view and I want to make it disappear whenever a pan is done. It works fine and on panning the scroll view disappears but the problem is now I can't scroll the contents.

[UIView animateWithDuration:0.2
                      delay:0.2
                    options: UIViewAnimationCurveLinear
                 animations:^{
                     slideView.frame=CGRectMake(268, 0, 500, 950);
                     curtain.frame=CGRectMake(0, 0, 268, 950);
                     curtain.backgroundColor=[[UIColor alloc]initWithRed:0 green:0 blue:0 alpha:0.6];
                     [self.view addSubview:slideView];
                    [self.view addSubview:curtain];
                     
                 } 
                 completion:^(BOOL finished){
                     NSLog(@"Done!");
                 }];

This is how I make my UIScrollView appear (slideView is a scroll view). And then I add a UIPanGestureRecognizer. It all works fine but scrolling is disabled. (panImage again hides my slideView.) How do I make scrolling work?

[slideView addGestureRecognizer:panImage];
Marler answered 4/2, 2012 at 9:0 Comment(0)
B
12

UIScrollView has its own pan gesture recognizer, which controls scrolling. By adding another pan gesture recognizer, you are preventing the scroll view's own pan gesture recognizer from working.

There are a number of ways to deal with this, but it would be helpful if you could explain how the system is supposed to know when the user is trying to dismiss the scroll view, and when he is trying to scroll, since you want a pan gesture to do both.

For example, you could set the delegates of both gesture recognizers to allow the recognizers to operate simultaneously (by overriding the gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: method). You could make each gesture recognizer require a different number of touches (by setting the minimumNumberOfTouches and maximumNumberOfTouches properties). You could use a UISwipeGestureRecognizer to recognize the dismiss gesture. You could detect when the user tries to scroll past the left edge of the scroll view by overriding the scrollViewDidScroll: method of the scroll view's delegate.

Benford answered 4/2, 2012 at 9:8 Comment(1)
You must recognize multiple gestures simultaneously, but you also need to disable the scrolling while handling your gesture. See @miroslav.Midland
H
3

You can set priorities of your panGestureRecognizers manually. This is how I solved the same problem: in delegate (BOOL)gestureRecognizerShouldBegin: I made scrolling disable in my scrollView if it should return YES. And in the beginning of a selector of my panGestureRecognizer I made scrolling enabled.

In this delegate you can create a condition of your panGestureRecognizer to enable such a way, that it will be work if and only if you want it to have a major priority.

Hornpipe answered 21/1, 2013 at 8:29 Comment(0)
I
1

Swift method,
Need to add UIGestureRecognizerDelegate to your class:

class ViewController: UIViewController, UIGestureRecognizerDelegate {

Need to set its delegate to self:

override func viewDidLoad() {
    super.viewDidLoad()
    var scrollView = UIScrollView(frame: self.view.frame)
    scrollView.delegate = self
}

Add this part in your class methods to active simultaneous gestures:

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}
Inefficiency answered 27/1, 2015 at 12:18 Comment(0)
S
0

When I had a view on top of the scrollView and I needed to add a gesture recognizer to make possible to scroll it in the view I did something like this (placed inside the viewDidAppear method):

hostedView.addGestureRecognizer(scrollView.panGestureRecognizer)
Stenophagous answered 13/1, 2021 at 10:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.