当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)
“动画”标志应该为假
我找到答案了!它需要在目标布局中覆盖targetContentOffsetForProposedContentOffset(proposedContentOffset:CGPoint)!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句