当 UICollectionView 完成交互式转换时,它会滚动到意外的偏移量。在完成 block 中设置 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)
“动画”标志应该为 false
最佳答案
我找到答案了!它需要在目标布局中覆盖 targetContentOffsetForProposedContentOffset(profusedContentOffset: CGPoint)!
关于swift - UICollectionViewTransitionLayout 和 contentOffset 完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38485115/