UIScrollview limit swipe area

ぃ、小莉子 提交于 2019-12-04 03:34:44

You can disable scrolling on the UIScrollView, override touchesBegan:withEvent: in your view controller, check if any of the touches began in the area where you'd like to enable swipes, and if the answer is 'yes', re-enable scrolling. Also override touchesEnded:withEvent: and touchesCancelled:withEvent: to disable scrolling when the touches are over.

Other answers didn't work for me. Subclassing UIScrollView worked for me (Swift 3):

class ScrollViewWithLimitedPan : UIScrollView {
    // MARK: - UIPanGestureRecognizer Delegate Method Override -
    override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        let locationInView = gestureRecognizer.location(in: self)
        print("where are we \(locationInView.y)")
        return locationInView.y > 400
    }
}

This blog post showcases a very simple and clean way of implementing the functionality.

// init or viewDidLoad

  UIScrollView *scrollView = (UIScrollView *)view;
  _scrollViewPanGestureRecognzier = [[UIPanGestureRecognizer alloc] init];
  _scrollViewPanGestureRecognzier.delegate = self;
  [scrollView addGestureRecognizer:_scrollViewPanGestureRecognzier];

//

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

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
  if (gestureRecognizer == _scrollViewPanGestureRecognzier)
  {
    CGPoint locationInView = [gestureRecognizer locationInView:self.view];
    if (locationInView.y > SOME_VALUE)
    {
      return YES;
    }
    return NO;
  }
  return NO;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!