gpt4 book ai didi

ios - Swift - 如果项目滚动一点,UICollection 回弹得太快

转载 作者:行者123 更新时间:2023-11-29 05:56:04 25 4
gpt4 key购买 nike

我试图让 UICollectionView 快速回到靠近它的任何项目单元格的中心,但问题是,当我稍微移动它时,它会以太快的速度弹回单元格的中心,而单元格的中心看起来并不光滑,是否可以忽略或禁用滚动,除非用户滑动到下一个项目并且不让 ScrollView 将其捕捉到同一项目?

这是我的捕捉代码

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

let layout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout
let cellWidthIncludingSpacing = layout.itemSize.width + layout.minimumLineSpacing
var offset = targetContentOffset.pointee

let index = (offset.x + scrollView.contentInset.left ) / cellWidthIncludingSpacing
let roundedIndex = round(index)

self.lastContentOffset = scrollView.contentOffset.x
offset = CGPoint(x: roundedIndex * cellWidthIncludingSpacing - scrollView.contentInset.left, y: -scrollView.contentInset.top)

targetContentOffset.pointee = offset


}

编辑:这就是它从物理设备恢复的方式。这里速度较慢,因为它是 gif

enter image description here

最佳答案

它会弹回来,因为您直接设置了 contentOffset。相反,您应该调用 UIScrollViewMethod setContentOffset(_:animated:)动画是真实的。如果您仍然不喜欢结果,请考虑实现 UICollectionLayout 方法 targetContentOffsetForProposedContentOffset:withScrollingVelocity:然后布局将自动捕捉到您返回的点。

关于ios - Swift - 如果项目滚动一点,UICollection 回弹得太快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55146146/

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