代码之家  ›  专栏  ›  技术社区  ›  Chris Conover

UICollectionView:展开单元格动画不一致

  •  2
  • Chris Conover  · 技术社区  · 8 年前

    enter image description here

    enter image description here

    有人知道这是否可以配置吗?

    违规代码(带有一些语法糖)是:

    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()
        })
    

    谢谢!

    0 回复  |  直到 8 年前