我的CustomFlowLayout中没有调用targetContentOffset(forProposedContentOffset :)

thi*_*ryb 1 uicollectionview uicollectionviewlayout

我正在编写一个自定义的UICollectionViewFlowLayout,它覆盖了targetContentOffset(forProposedContentOffset :),以便在用户捏缩放时提供正确的contentOffset,因为我遇到了这个问题(内容错误)

在UICollectionView上动态设置布局会导致无法解释的contentOffset更改

class TimelineCollectionViewFlowLayout: UICollectionViewFlowLayout {

        // MARK: - Init

        override init() {
            super.init()
            self.minimumLineSpacing = 0
        }

        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
        }

        override func prepare() {
            if let collectionView = self.collectionView {
                collectionView.isPagingEnabled = false
                self.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
                self.scrollDirection = .horizontal
            }
        }

    override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
        print("hello")
        return proposedContentOffset
    }
}
Run Code Online (Sandbox Code Playgroud)

但不幸的是没有调用targetContentOffset,你知道为什么?

谢谢!

蒂埃里

小智 8

尝试

override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint 
Run Code Online (Sandbox Code Playgroud)