在尝试“修复”我不一致的扩展收集单元动画的时间令人沮丧之后,我确定UICollectionView / FlowLayout使用不同的动画,具体取决于单元格扩展的比例,阈值为2:1。
如果比例低于2:1,你会得到:
如果比率等于或大于2:1,则得到:
有谁知道这是否可以配置?
违规代码(含有一些语法糖)是:
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
let cell = SimpleCalculatedSizeCell.prototype
let contents = data[indexPath.item]
cell.label.text = contents
let width = collectionView.bounds
.inset(collectionView.contentInset)
.inset(layout.sectionInset)
.width
let finalSize = cell.systemLayoutSizeFitting(
.init(width: width, height: 0),
withHorizontalFittingPriority: .required,
verticalFittingPriority: .fittingSizeLevel)
.withWidth(width)
if let selected = selected, selected == indexPath {
print("\(#function): \(finalSize)")
return finalSize.withHeight(150)
}
print("\(#function): \(finalSize)")
// return finalSize.withHeight(76) // expands smoothly
return finalSize.withHeight(75) // fades and snaps
}
选择处理:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
selected = selected == indexPath ? nil : indexPath
UIView.animate(withDuration: 0.25, animations: {
collectionView.collectionViewLayout.invalidateLayout()
collectionView.layoutIfNeeded()
})
谢谢!