【问题标题】:Change UICollectionView's bounce start/end points更改 UICollectionView 的反弹起点/终点
【发布时间】:2019-04-20 16:12:24
【问题描述】:

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

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

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

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

我仍然面临的问题是,当到达 contenSize 端时,反弹动画不像原始反弹那样平滑。

任何想法缺少什么?代码如下:sn-p:

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]

【问题讨论】:

  • 我从未尝试过,但我认为您必须重写 scrollViewDidEndDraging 方法并创建自己的逻辑或弹跳动画,这很容易模仿或重新创建
  • 我认为调整你的内容大小比试图改变视图的行为更好。

标签: ios swift xcode uicollectionview bounce


【解决方案1】:

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

我只需要设置 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    相关资源
    最近更新 更多