How to add UIPanGestureRecognizer to UIScrollView subviews
Asked Answered
B

3

9

I am adding subviews to a UIScrollView and then I add UIPanGestureRecognizer to these subviews. Everything works fine but now after adding UIPanGestureRecognizer to subviews of the scroll view, scrolling is not possible.

What can be the possible solution to this issue?

Balsaminaceous answered 4/2, 2012 at 12:17 Comment(4)
Where do you add the subviews and where do you attach the pan gesture recognizer?Kamerman
possible duplicate of Scrolling is disabled when i add A UIPangesturerecognizer to UIScrollViewInspiration
You asked exactly the same question three hours before asking this one.Inspiration
I've solved this not using Pan gesture recognizers and overriding TouchesBegan, TouchesMove, TouchesEnded, TouchesCancelled methods (in xamarin). And in subview, when I want to Move/Pan I disable scrolling to UIScrollView super view.Josephjosepha
D
22

The problem is that the pan gesture recognizer is what is used in the scroll view to control scrolling. Your gesture recogniser is taking priority and disabling the scroll views

If you want to always be able to scroll you can set your gesture recogniser to require the scroll views one to fail before it will work:

[self.myCustomPanRecognizer requireGestureRecognizerToFail:self.scrollView.panGestureRecognizer]; 

Edit: as Bastian pointed out in the comments, the reference to pan guesture is only in iOS 5, prior to this, check the array of gesture recognisers and find the one of type UIPanGestureRecognizer

if you want both to work you may need to do something to separate your recogniser from the scroll views, e.g. make the user tap and hold before your custom recogniser will be recognised.

there is also a delegate method which will allow both recognisers to work together, but I'm not sure how well this works when both are the same type

Daphie answered 4/2, 2012 at 13:25 Comment(3)
scrollview.panGestureRecognizer is only available from ios5 .. if you want to support ios4 you can get the gesture recognizers from the scrollview and check the class to get the pangesturerecognizer.Erasmo
thanks @Bastian, i didn't realise it was iOS 5 only, answer updatedDaphie
what if we want to reallow self.scrollView.panGestureRecognizerMagniloquent
E
10

If you want to use both simultaneus you can use

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

from the delegate, but that is probably not what you want ;)

Erasmo answered 4/2, 2012 at 18:49 Comment(4)
Unfortunately this doesn't work with gesture recognizer's that belong to the scroll view. These recognizer's require that their associated scroll view also be their delegate.Cadelle
well if you can't change the delegate of the gesture recognizer in the scrollview you might still change the delegate of the other recognizer.Erasmo
Yes this is true, but you need both of the coordinated recognizers to return YES to the delegate method, I believe.Cadelle
well you can do something like: - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { if ([gestureRecognizer isEqual:yourGestureRcognizer] && [otherGestureRecognizer isEqual:scrollView.panGestureRecognizer]) { return YES; } else { //other calculations } }Joyner
C
1

Swift 4

Conform the view controller to the UIGestureRecognizerDelegate...

SomeViewController: UIViewController, UIGestureRecognizerDelegate {
    ...
}

...set the view controller as the custom pan gesture recognizer's delegate...

customPanGestureRecognizer.delegate = self

...and using the simultaneous delegate, allow the custom panner and the scroll view's (or table view's) panner to operate simultaneously...

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {

    if otherGestureRecognizer == scrollView.panGestureRecognizer { // or tableView.panGestureRecognizer
        return true
    } else {
        return false
    }

}

There are two other methods that ask the delegate if a gesture recognizer should require another gesture recognizer to fail or if a gesture recognizer should be required to fail by another gesture recognizer. You will likely need further optimization beyond this but this is the starting point.

Cordi answered 16/12, 2018 at 12:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.