UIScrollview限制滑动区域

时间:2011-12-04 23:47:41

标签: ios xcode uiscrollview swipe

我正在尝试限制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上将delayedContentTouches设置为NO

self.categoryScrollView.delaysContentTouches = NO;

3 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:6)

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

答案 2 :(得分:4)

This blog post展示了一种实现功能的简单而简洁的方法。

// 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;
}
相关问题