OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在启用对角 setContentOffset 滚动动画的同时禁用手动对角滚动 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 12:11
标题: ios - 如何在启用对角 setContentOffset 滚动动画的同时禁用手动对角滚动

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

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

- (void)scrollViewWillBeginDraggingUIScrollView *)scrollView  {
    self.offset = self.collectionView.contentOffset;
}

// control scroll to only horizontal & vertical
- (void)scrollViewDidScrollUIScrollView *)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];

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

如何处理?



Best Answer-推荐答案


在 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)
        }
    }
}

关于ios - 如何在启用对角 setContentOffset 滚动动画的同时禁用手动对角滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26545983/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4