【问题标题】:How to disable manually diagonal scrolling WHILE enable diagonal setContentOffset scroll annimation如何在启用对角 setContentOffset 滚动动画的同时禁用手动对角滚动
【发布时间】:2014-12-20 04:15:21
【问题描述】:

我正在使用集合视图。 虽然 directionLockEnabled 可以设置为 YES,但对角滚动仍然是启用的。

所以我在某处找到了解决方案:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView  {
    self.offset = self.collectionView.contentOffset;
}

// control scroll to only horizontal & vertical
- (void)scrollViewDidScroll:(UIScrollView *)scrollView  {
    CGFloat deltaX = ABS(self.offset.x - self.collectionView.contentOffset.x);
    CGFloat deltaY = ABS(self.offset.y - self.collectionView.contentOffset.y);
    if (deltaX != 0 && deltaY != 0) {
        if (deltaX >= deltaY) {
            self.collectionView.contentOffset = CGPointMake(self.collectionView.contentOffset.x, self.offset.y);
        }
        else    {
            self.collectionView.contentOffset = CGPointMake(self.offset.x, self.collectionView.contentOffset.y);
        }
    }
}

但是副作用是当我用 x, y > 0 调用时

[self.collectionView setContentOffset:CGPointMake(x, y) animated:animated];

由于上面的代码块,它根本不滚动。

如何处理?

【问题讨论】:

    标签: ios objective-c cocoa-touch uiscrollview uikit


    【解决方案1】:

    在 Swift 中禁用手动对角滚动:

    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        self.offset = scrollView.contentOffset        
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let deltaX = abs(self.offset.x - self.collectionView.contentOffset.x)
        let deltaY = abs(self.offset.y - self.collectionView.contentOffset.y)
    
        if deltaX != 0 && deltaY != 0 {
            if deltaX >= deltaY {
                self.collectionView.contentOffset = CGPoint(x: self.collectionView.contentOffset.x, y: self.offset.y)
            } else {
                self.collectionView.contentOffset = CGPoint(x: self.offset.x, y: self.collectionView.contentOffset.y)
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      好吧,我想通了... scrollView.dragging 可以区分手动滚动还是自动滚动...

      【讨论】:

        【解决方案3】:

        你可以的

        collectionView.isScrollEnabled = false
        

        它将禁止用户滚动,但您将能够以编程方式滚动使用

        collectionView.scrollToItem(at:at:animated:)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-05
          • 1970-01-01
          • 2011-04-25
          相关资源
          最近更新 更多