UIPanGestureRecognizer conflicts with scrollview

I am trying to add a gesture recognizer to a view containing scrollview, but I think I have problems with priorities.

My global UIView has a UIPanGestureRecognizer set as follows:

_bottomPanGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(bottomPanGestureDetected:)]; _bottomPanGestureRecognizer.minimumNumberOfTouches = 2; _bottomPanGestureRecognizer.maximumNumberOfTouches = 2; _bottomPanGestureRecognizer.delaysTouchesBegan = NO; _bottomPanGestureRecognizer.delaysTouchesEnded = NO; 

I want to recognize this gesture in order to display another from bottom to bottom with some kind of reproach down.

The problem is that scrollview recognizes its own gestures are tough in front of mine.

So, I tried to postpone it thanks to:

 [_scrollView.panGestureRecognizer requireGestureRecognizerToFail:_bottomPanGestureRecognizer]; 

And it works, the scrollview event is fired after my two fingers to recognize, but the problem occurs when I use only one finger to scroll in the scroll, the scroll works after a short delay.

I would not want to linger on this event, is this possible? Any idea is welcome!

Greetings.

Kirill

+6
source share
2 answers

If it is not solved yet, I solved the problem for me.

I added a UIPanGestureRecognizer to the UIScrollView to detect two finger gestures, and the default behavior of the UIScrollView (scroll to something) still works.

So what I did was add the UIPanGestureReconizer to the UIScrollView:

 UIPanGestureRecognizer *pangestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(displayReloadIndicator:)]; pangestureRecognizer.minimumNumberOfTouches = 2; pangestureRecognizer.delegate = self; [self.scrollView addGestureRecognizer:pangestureRecognizer]; [pangestureRecognizer release]; 

After that, I added the code:

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

After that I applied the action method of gesture recognition recognizers.

 - (void) displayReloadIndicator:(UIPanGestureRecognizer*) panGestureRecognizer { UIGestureRecognizerState gestureRecognizerState = gestureRecognizer.state; CGPoint translation = [gestureRecognizer translationInView:self.scv_bibgesamt]; if (gestureRecognizerState == UIGestureRecognizerStateBegan) { // create a UIView with all the Pull Refresh Headers and add to UIScrollView // This is really much lines of code, but its simply creating a UIView (later you'll find a myRefreshHeaderView, which is my base view) and add UIElements eg UIActivityIndicatorView, a UILabel and a UIImageView on it // In iOS 6 you will also have the possibility to add a UIRefreshControl to your UIScrollView } else if (gestureRecognizerState == UIGestureRecognizerStateEnded || gestureRecognizerState == UIGestureRecognizerStateCancelled) { if (translation.y >= _myRefreshHeaderView.frame.size.height + 12) { // _myRefreshHeaderView is my baseview //so the UIScrollView has been dragged down with two fingers over a specific point and have been release now, so we can refresh the content on the UIScrollView [self refreshContent]; //animatly display the refresh view as the top content of the UIScrollView [self.scrollView setContentOffset:CGPointMake(0, myRefreshHeaderView.frame.size.height) animated:YES]; } else { //the UIScrollView has not been dragged over a specific point so don't do anything (just scroll back to origin) [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES]; //remove the view (because it no longer needed) [_myRefreshHeaderView removeFromSuperview]; } } 

UPDATE:

If you want to integrate drag and drop functionality from your navigation controller, you must integrate the following code:

 - (void) viewDidLoad { [super viewDidLoad]; if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) { self.navigationController.interactivePopGestureRecognizer.enabled = YES; self.navigationController.interactivePopGestureRecognizer.delegate = nil; } //setup view controller } 

and

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { if (gestureRecognizer == _panGestureRecognizer && [self.navigationController.viewControllers count] > 1) { CGPoint point = [touch locationInView:self.view.window]; if (point.x < 20 || point.x > self.view.window.frame.size.width - 20) { return NO; } } return YES; } 
+7
source

Deploy panRecognizer delegate to recognize UIScrollView UIGestureRecognizer simultaneously

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { if (_panRecognizer == gestureRecognizer) { if ([otherGestureRecognizer.view isKindOfClass:UIScrollView.class]) { UIScrollView *scrollView = (UIScrollView *)otherGestureRecognizer.view; if (scrollView.contentOffset.x == 0) { return YES; } } } return NO; } 
+1
source

All Articles