gpt4 book ai didi

ios - 更改 UICollectionView 的弹跳起点/终点

转载 作者:行者123 更新时间:2023-11-28 07:35:14 33 4
gpt4 key购买 nike

通常 UICollectionView 在滚动超过它的 contentSize 时开始反弹,即当 contentOffset < 0 时或 contentOffset > contentSize.width用于水平方向。

是否可以更改此行为,以便在滚动到第 10 项(当 contentOffset < itemSize.Width * 10contentOffset > contentSize.width - (itemSize.Width * 10) 时)时启动反弹效果?

更新 1:
@OverD - 感谢您为我指明正确的方向。

我在 scrollViewWillEndDragging 完成了一些工作和调整targetContentOffset必要时。

我仍然面临的问题是当到达 contenSize 端时,弹跳动画不像原来的弹跳那样流畅

知道缺少什么吗?下面的代码片段:

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

let numberOfBouncingCells = 10
let extraSectionWidth = (collectionViewFlowLayout.itemSize.width + collectionViewFlowLayout.minimumLineSpacing) * numberOfBouncingCells

let startXOffset = extraSectionWidth
let endXOffset = collectionView.contentSize.width - 2 * extraSectionWidth

let yOffset = collectionView.contentOffset.y

if targetContentOffset.pointee.x < startXOffset {
targetContentOffset.pointee = CGPoint(x: startXOffset, y: yOffset)
} else if targetContentOffset.pointee.x > endXOffset {
targetContentOffset.pointee = CGPoint(x: endXOffset, y: yOffset)
}

}

更新 2(用于回答):

请参阅下面的答案,我放弃了 scrollViewWillEndDragging有利于简单改变的方法 collectionView.contentInset [.left, .right]

最佳答案

我终于回答了我自己的问题。

我只需设置 collectionView.contentInset [.left, .right] 即可实现预期的行为。

var collectionViewFlowLayout: UICollectionViewFlowLayout {
return collectionView.collectionViewLayout as! UICollectionViewFlowLayout
}

let numberOfBouncingCells = 10
let extraBouncingWidth = (collectionViewFlowLayout.itemSize.width + collectionViewFlowLayout.minimumLineSpacing) * numberOfBouncingCells

collectionView.contentInset.left = -extraBouncingWidth
collectionView.contentInset.right = -extraBouncingWidth

关于ios - 更改 UICollectionView 的弹跳起点/终点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53355360/

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