Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have a UIScrollView scroll and have a gesture recognizer?

Tags:

I have a gesture recognizer on a UIScrollView, however it hardly ever gets called as the UIScrollView eats all the gestures.

I partially got around this issue with this line: [scrollView.panGestureRecognizer requireGestureRecognizerToFail:rightSwipe]; however, this line results in my recognizer always being accepted (the desired behavior) and the scroll view not scrolling.

That is, when you scroll, the recognizer is accepted but the view doesn't scroll.

How can I get around this, or is there an alternate solution?

Thanks!

like image 625
Phillip Avatar asked May 24 '14 03:05

Phillip


People also ask

How do I add a gesture recognizer to a view?

A gesture recognizer operates on touches hit-tested to a specific view and all of that view's subviews. It thus must be associated with that view. To make that association you must call the UIView method addGestureRecognizer(_:) . A gesture recognizer doesn't participate in the view's responder chain.

What is scroll view in Swift?

The scroll view displays its content within the scrollable content region. As the user performs platform-appropriate scroll gestures, the scroll view adjusts what portion of the underlying content is visible. ScrollView can scroll horizontally, vertically, or both, but does not provide zooming functionality.


2 Answers

Make a subclass of UIScrollView. Add this method in your new subclass

- (BOOL)gestureRecognizer:(UIPanGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UISwipeGestureRecognizer *)otherGestureRecognizer  {     return YES; } 

Make your scrollView class to your new scrollview subclass.

like image 136
Warif Akhand Rishi Avatar answered Oct 09 '22 01:10

Warif Akhand Rishi


The way that works for me is subclass UIScrollView and conform to the UIGestureRecognizerDelegate in that subclass. Then call this method.

class ATScrollView: UIScrollView, UIGestureRecognizerDelegate {      func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,                            shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {         return true     }  } 
like image 27
abe973t Avatar answered Oct 09 '22 02:10

abe973t