gpt4 book ai didi

ios - 只向右滚动的 UICollectionView(没有向左、向上或向下滚动)

转载 作者:行者123 更新时间:2023-11-29 12:55:25 26 4
gpt4 key购买 nike

您将如何着手实现具有这种行为的 UICollectionView?这个想法是,一旦用户导航到某个点,他们就不能返回并再次查看这些单元格。

我尝试的解决方案是监听 Collection View 上的手势,如果在元素上发生滑动,则禁用滚动。这样做的明显问题是用户可以简单地按住并拖动任何特定的单元格。

有什么想法吗?

最佳答案

我认为这种行为可能会让您的用户感到困惑。

也许您应该尝试添加一些弹性/弹跳,这样您的用户就不会那么困惑了。

无论如何,我看到了两种不同的方法来实现这一目标而无需子类化

1/由于 UICollectionViewDelegate 符合 UIScrollViewDelegate,您可以使用 – scrollViewWillBeginDragging: 获取 ScrollView 的起始偏移量,然后在 – scrollViewDidScroll: 中比较新偏移量的 x值(value)。如果新的 offset.x 的值小于起始值,则将其设置为 0 并更新 ScrollView 。

#pragma mark - UIScrollViewDelegate

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

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

CGPoint newOffset = scrollView.contentOffset;

if (newOffset.x < offset.x) {
// scrolling to the left, reset offset
[scrollView setContentOffset:offset];
}
}

因为在 iOS 中滚动有惯性,scrollViewDidScroll: 被调用了很多时间,所以它可能会导致性能问题。您可以通过使用来自 UIScrollViewDelegate 的 scrollViewWillEndDragging:withVelocity:targetContentOffset: 定位偏移量来减少调用次数。

2/或者你可以只使用方法 scrollViewWillEndDragging:withVelocity:targetContentOffset: 我刚才谈到的,它通过动画将偏移量设置回它的开头。

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {

CGPoint newOffset = scrollView.contentOffset;

if (newOffset.x < offset.x) {
// scrolling to the left, reset offset with animation
targetContentOffset->x = offset.x;
}
}

3/你谈到了 UISwipeGestureRecognizer,你尝试过 UIPanGestureRecognizer 吗?这就是“简单的按住并拖动”。

关于ios - 只向右滚动的 UICollectionView(没有向左、向上或向下滚动),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21272019/

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