UIScrollview限制滑动区域

我试图限制UIScrollview的滑动区域,但我无法做到这一点。

我想将滑动区域设置为UIScrollview的顶部,但我想将所有内容设置为可见。

更新:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { if ([touches count] > 0) { UITouch *tempTouch = [touches anyObject]; CGPoint touchLocation = [tempTouch locationInView:self.categoryScrollView]; if (touchLocation.y > 280.0) { NSLog(@"enabled"); self.categoryScrollView.scrollEnabled = YES; } } [self.categoryScrollView touchesBegan:touches withEvent:event]; } - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { // [super touchesEnded:touches withEvent:event]; self.categoryScrollView.scrollEnabled = YES; [self.categoryScrollView touchesBegan:touches withEvent:event]; } 

解决方案:别忘了在UIScrollView上将delaysContentTouches设置为NO

 self.categoryScrollView.delaysContentTouches = NO; 

您可以在UIScrollView上禁用滚动,在视图控制器中覆盖touchesBegan:withEvent:检查是否在您要启用滑动的区域中开始任何触摸,如果答案为“是”,则重新启用滚动。 还覆盖touchesEnded:withEvent:touchesCancelled:withEvent:以在触摸结束时禁用滚动。

这篇博客文章展示了一种非常简单而干净的实现function的方法。

 // 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; } 

其他答案对我不起作用。 子类化UIScrollView为我工作(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 } }