Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add functionality to standard Pan Gesture Recognizer in a UIScrollView

I am trying to keep track of where a finger is in a UIScrollView. I have subclassed UIScrollView (see below) but unfortunately the gesture recognizer that I am adding is overriding the standard one.

As a result I get NSLog(@"Pan") to work but unfortunately the view doesn't scroll anymore.

How can I get both gestures recognizers to work at the same time?

Thanks.

- (void)viewDidLoad:(BOOL)animated
{
    [super viewDidLoad:animated];

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [scrollView addGestureRecognizer:panRecognizer];
}


- (void)pan:(id)sender {
    NSLog(@"Pan");
}
like image 284
ios-lizard Avatar asked Dec 04 '11 17:12

ios-lizard


2 Answers

If you want it not to override the standard one you just have to allow both to be simultaneously recognized.

- (void)viewDidLoad:(BOOL)animated
{
    [super viewDidLoad:animated];

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    panRecognizer.delegate = self;
    [scrollView addGestureRecognizer:panRecognizer];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
     return TRUE;
}


- (void)pan:(id)sender {
    NSLog(@"Pan");
}
like image 164
Michal Palczewski Avatar answered Nov 05 '22 20:11

Michal Palczewski


EDIT: this method works! You just need to set canCancelContentTouches as soon as possible (I do it in viewDidLoad).

ORIGINAL ANSWER: I have tried a new approach but unfortunately it doesn't fully work.

Instead of adding a gesture recognizer I am subclassing the UIScrollView and writing my own touchesBegan, touchesMoved, etc methods.

This way I know where the user is touching BUT unfortunately the PanGestureRecognizer is triggering touchesCancelled every time I start to scroll even after setting the canCancelContentTouches to NO.

Does anybody know why? I have also found this.

like image 4
ios-lizard Avatar answered Nov 05 '22 19:11

ios-lizard