UICollectionViewTransitionLayout 和 contentOffset 完成

UICollectionViewTransitionLayout and contentOffset on finish

当 UICollectionView 完成交互式过渡时,它滚动到意外的偏移量。在转换的完成块中设置 contentOffset 有帮助,但我在瞬间看到了相同的无效 contentOffset。无论是使用标准 UICollectionViewFlowLayout 还是自定义布局。有没有办法坚持下一个布局的单元格?我的代码示例

let nextLayout = self.collectionView.collectionViewLayout === layout1 ? layout2 : layout1
        let transition = collectionView.startInteractiveTransitionToCollectionViewLayout(nextLayout) { (par1: Bool, par2: Bool) in
            //There was contentOffset setting code, that leads to blink
        }

        let initializer:(POPMutableAnimatableProperty!)->Void = {property in
            property.readBlock = {obj, values in
                values[0] = transition.transitionProgress
            }
            property.writeBlock = {obj, values in
                transition.transitionProgress = values[0]
                if let attrs: UICollectionViewLayoutAttributes = transition.layoutAttributesForItemAtIndexPath(indexPath) {
                    collectionView.contentOffset = CGPointMake(attrs.frame.origin.x, attrs.frame.origin.y)
                }
            }
            property.threshold = 0.01
        }

        let springAnimation = POPSpringAnimation()
        let property = POPAnimatableProperty.propertyWithName("com.pop.property", initializer: initializer) as! POPAnimatableProperty
        springAnimation.springBounciness = 8.0
        springAnimation.dynamicsMass = 8.0
        springAnimation.property = property
        springAnimation.fromValue = 0.0
        springAnimation.toValue = 1.0

        springAnimation.completionBlock = {anim, finished in
            if finished {
                transition.transitionProgress = 1.0
                self.collectionView.finishInteractiveTransition()
            }
        }
        transition.pop_addAnimation(springAnimation, forKey: "transitionProgress")

更新: 在开始时添加此代码对我有帮助,但我需要在转换前不进行额外滚动的情况下执行转换

collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredVertically, animated: false)

"animated" 标志应为 false

我找到答案了!它需要覆盖目标布局中的 targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint)!