UIScrollview 限制滑动区域

Posted

技术标签:

【中文标题】UIScrollview 限制滑动区域【英文标题】:UIScrollview limit swipe area 【发布时间】:2011-12-04 23:47:40 【问题描述】:

我正在尝试限制 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;

【问题讨论】:

不错的一个! 2 年后你帮助了我:P 【参考方案1】:

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

【讨论】:

您好,出了点问题。滑动仅在触摸结束后启用,而不是在当前触摸阶段:( @ViskyMáté 我不确定它是否会起作用,但我认为值得一试。要尝试的另一件事是保持滚动启用,如果它们在该区域之外,请在 touchesMoved:withEvent: 中禁用它,然后在 touchesEnded/touchesCancelled 中重新启用滚动。 它开始工作了,但是:(它一直没有工作。在 touchBegin/Cancel/Moved 触发之前有一个短暂的延迟,这就是为什么如果我快速捏,它不会' t 工作,任何其他时间都在工作。如果你能帮我解决这个延迟(我不知道那在哪里),那就太好了,并且成功的解决方案 @ViskyMáté 好吧,我猜这是一种改进。不幸的是,您无法关闭识别延迟,但您可以做另一件事:跟踪当前正在进行的所有触摸事件,并在任何触摸开始超出指定区域时禁用滚动。此外,我认为不将 touchesBegan / touchesEnded / 等传递给您的 categoryScrollView 是可以的。我知道自定义滚动可能会很棘手,但我认为您的方向是正确的:我使用了类似的策略来实现在滚动视图中拖动对象。 self.categoryScrollView.delaysContentTouches = NO;【参考方案2】:

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

【讨论】:

我认为这是“最干净”的方式。 顺利。【参考方案3】:

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;

【讨论】:

以上是关于UIScrollview 限制滑动区域的主要内容,如果未能解决你的问题,请参考以下文章

扩展 UIScrollView 交互区域,区分滑动和点击

UISwipeGestureRecognizer 和 UIScrollView

如何使用滑动手势作为触发器移动 UIScrollView

使用 UIPanGestureRecognizer 滑动视图时如何限制区域?

限制在 UIScrollView 上识别的触摸

html手机页面 限制滑动区域(在滑动一个div区域时不会带动整个页面的滑动,详情见补充)