完成时的UICollectionViewTransitionLayout和contentOffset

时间:2016-07-20 15:46:52

标签: swift uicollectionview uicollectionviewlayout

当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)

"动画"标志应该是假的

1 个答案:

答案 0 :(得分:3)

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