How to recognize swipe gesture in UIScrollView

前端 未结 4 597
甜味超标
甜味超标 2020-11-28 21:54

I\'m trying to recognize left/right swipe gesture in a UIScrollView. I\'ve tried to create UISwipeGestureRecognizers and associate them with the sc

相关标签:
4条回答
  • 2020-11-28 22:38
    UIScrollView *scrollView = ...
    UISwipeGestureRecognizer *mySwipe = ...
    

    The correct solution to fix this issue is to add one line of code:

    [scrollView.panGestureRecognizer requireGestureRecognizerToFail:mySwipe]
    

    Swift version:

    scrollView.panGestureRecognizer.requireGestureRecognizerToFail(mySwipe)
    

    Swift4 version:

    scrollView.panGestureRecognizer.require(toFail: mySwipe!);
    
    0 讨论(0)
  • 2020-11-28 22:43

    For those who want to animate and customize their swipe gesture recognizers.

    We can use UIScrollView and UIGestureRecognizer delegates:

     Class ViewController: UIViewController, UISCrollViewDelegate, UIGestureRecognizerDelegate { 
    
    
       override func viewDidLoad() {
        super.viewDidLoad()
    
        scrollView.delegate = self
        swipeLeft.delegate = self
        swipeRight.delegate = self
    
      }
    
    
      func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        return true
      }
    
      func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
      }
    
      func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return scrollView.alwaysBounceHorizontal
      }
    
    
      func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    
        // Your custom animation at the end of scrolling.
      }
    }
    
    0 讨论(0)
  • 2020-11-28 22:45

    Good post.

    I was doing a similar thing (no image view) and I basically had to disable scrolling if the contentSize was smaller than the height (my scroll view only scrolls vertical).

    if (scrollView.contentSize.height>scrollView.frame.size.height) {
        scrollView.scrollEnabled = YES;
    }
    else {
        scrollView.scrollEnabled = NO;
    }
    

    That did the trick for me

    0 讨论(0)
  • 2020-11-28 22:58

    Figured it out. In my case, my UIScrollView contained a UIImage that I allowed zooming. Apparently that meant that scrolling is enabled and the UIScrollView had trouble distinguishing between gestures intended to scroll vs. swipe (next, previous image).

    The key in my case, is to disable scrolling in the scroll view when the image is not zoomed in, and renabled it when it is zoomed in. This provides the expected behavior.

    The critical piece is to put the following in the scroll view's delegate:

    - (void)scrollViewDidZoom:(UIScrollView *)scrollView {
      if (scrollView.zoomScale!=1.0) {
        // Zooming, enable scrolling
        scrollView.scrollEnabled = TRUE;
      } else {
        // Not zoomed, disable scrolling so gestures get used instead
        scrollView.scrollEnabled = FALSE;
      }
    }
    

    I also have to initialize the scroll view with scrolling disabled. To enable zooming, simply provide an image on a delegate call,

    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
      // Return the scroll view
      return myImage;
    }
    

    And set a few parms in viewDidLoad for the zooming and setup gesture recognizers as well

    - (void)viewDidLoad {
      [super viewDidLoad];
      myScrollView.contentSize = CGSizeMake(myImage.frame.size.width, myImage.frame.size.height);
      myScrollView.maximumZoomScale = 4.0;
      myScrollView.minimumZoomScale = 1.0;
      myScrollView.clipsToBounds = YES;
      myScrollView.delegate = self;
    
      [myScrollView addSubview:myImage];
      [self setWantsFullScreenLayout:TRUE];
    
      myScrollView.scrollEnabled = FALSE; 
      UISwipeGestureRecognizer *recognizer = 
        [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
      recognizer.delaysTouchesBegan = TRUE;
      [myScrollView addGestureRecognizer:recognizer];
      [recognizer release];
    
      recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
      recognizer.direction = UISwipeGestureRecognizerDirectionLeft;
      [myScrollView addGestureRecognizer:recognizer];
      [recognizer release];
      [myScrollView delaysContentTouches];
    }
    
    0 讨论(0)
提交回复
热议问题