gpt4 book ai didi

ios - 如何在启用对角线 setContentOffset 滚动动画时禁用手动对角线滚动

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:03:15 32 4
gpt4 key购买 nike

我正在使用 Collection View 。尽管可以将 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];

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

如何处理?

最佳答案

要在 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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com