ios - 在布局更改的同时重新加载/动画化 UICollectionView

标签 ios swift uicollectionview uicollectionviewcell uicollectionviewlayout

This question (及其答案)让我达到了我想要的一半。

但现在我陷入了下一部分,(老实说)我什至不确定这是否可能。

我已经这样定义了一个单元格...

class ExpandableCell: UICollectionViewCell {

    var priority: Expandable.Priority = .low {
        didSet {
            imageView.isHidden = priority != .high
        }
    }

    private let imageView: UIView = {
        let v = UIView()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.backgroundColor = .red
        v.heightAnchor.constraint(equalTo: v.widthAnchor).isActive = true
        return v
    }()

    private let label: UILabel = {
        let l = UILabel()
        l.translatesAutoresizingMaskIntoConstraints = false
        l.font = UIFont.preferredFont(forTextStyle: .headline)
        l.numberOfLines = 0
        l.text = "Tap Me!"
        l.setContentCompressionResistancePriority(.required, for: .vertical)
        l.adjustsFontForContentSizeCategory = true
        l.textAlignment = .center
        return l
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)

        let labelStack: UIStackView = {
            let s = UIStackView(arrangedSubviews: [label])
            s.translatesAutoresizingMaskIntoConstraints = false
            s.axis = .vertical
            s.alignment = .center
            s.distribution = .equalCentering
            s.isLayoutMarginsRelativeArrangement = true
            return s
        }()

        let stackView: UIStackView = {
            let s = UIStackView(arrangedSubviews: [imageView, labelStack])
            s.translatesAutoresizingMaskIntoConstraints = false
            s.axis = .horizontal
            s.spacing = 10
            s.layoutMargins = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
            s.alignment = .fill
            s.distribution = .fill
            s.isLayoutMarginsRelativeArrangement = true
            return s
        }()

        addSubview(stackView)

        NSLayoutConstraint.activate([
            stackView.topAnchor.constraint(equalTo: topAnchor),
            stackView.bottomAnchor.constraint(equalTo: bottomAnchor),
            stackView.leadingAnchor.constraint(equalTo: leadingAnchor),
            stackView.trailingAnchor.constraint(equalTo: trailingAnchor),
            ])

        clipsToBounds = true

        backgroundColor = UIColor.init(white: 0.8, alpha: 1.0)

        layer.cornerRadius = 6
    }

    func display(button: DigestButton) {
        label.text = button.title
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
        super.apply(layoutAttributes)
        layoutIfNeeded()
    }
}

它有三种可能的状态(小、中或大),由优先级设置。

我正在更新 Collection View 的布局,以根据存储在数据中的优先级更新大小。

它是这样工作的……

enter image description here

从这段代码可以看出...

var priority: Expandable.Priority = .low {
    didSet {
        imageView.isHidden = priority != .high
    }
}

更新优先级将显示/隐藏单元格中的“imageView”。它只是用于概念验证的标准 UIView

但它只是在布局完成更新后显示/隐藏 imageView

根据链接的问题,我正在使用代码更新事物的布局...

collectionView.performBatchUpdates(nil) { _ in
    // completion block
    collectionView.reloadItems(at: changedIndexPaths)
}

我正在为完成 block 中更改的单元格重新加载数据。 (因此在动画结束后进行更新)。

如果我这样移动它...

collectionView.performBatchUpdates({
    collectionView.reloadItems(at: changedIndexPaths)
}) { _ in
    // completion block
}

然后动画变成这样...

enter image description here

...这比我开始的时候更糟糕。

有没有一种方法可以使单元格状态的变化具有动画效果?理想情况下,我希望 ImageView 以与布局动画相同的速率进行动画处理。

如果我不更改单元格内的内容,那么这不是问题。但在某些情况下,我会想要更改单元格内的内容,并且我希望无论哪种方式都能顺利过渡。

最佳答案

好的...我在这里添加这个作为初步答案,但我想我刚刚破解了它。

诀窍是通过 UICollectionViewLayoutAttributes 将布局动画期间所需的任何信息传递到单元格中。

从文档中可以看出,您可以将布局属性子类化,以在其中添加布局所需的附加信息。

通过将 priority 信息放入布局属性中,我现在可以在单元格移动和调整大小的同时为进入 View 的 ImageView 设置动画。

轰! :D

好的...像这样子类化 UICollectionViewLayoutAttributes...

fileprivate class PriorityLayoutAttributes: UICollectionViewLayoutAttributes {
    var priority: DigestExpandable.Priority? = nil

    override func copy(with zone: NSZone? = nil) -> Any {
        let copy = super.copy(with: zone) as! PriorityLayoutAttributes
        copy.priority = self.priority
        return copy
    }

    override func isEqual(_ object: Any?) -> Bool {
        guard let rhs = object as? PriorityLayoutAttributes else {
            return false
        }

        return super.isEqual(rhs) && priority == rhs.priority
    }
}

... 然后为这个特定的单元格使用这些属性。

在单元格中,我像这样更新了 apply(_) 函数...

override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
    super.apply(layoutAttributes)

    if let attributes = layoutAttributes as? PriorityLayoutAttributes,
        let priority = attributes.priority {
        self.priority = priority
    }

    layoutIfNeeded()
}

现在它可以很好地工作和动画了:D

enter image description here

关于ios - 在布局更改的同时重新加载/动画化 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51236002/

相关文章:

ios - 无法在 Mavericks 下使用我自己的 kestore 和 xcodebuild

iOS:编辑选项卡位置后导航到特定选项卡

ios - iCloud 和鞋盒应用程序 : How to atomically sync a file package?

ios - Xcode 7 归档失败

ios - swift : Play Video in Landscape Mode When Fullscreen

ios - Swift 私有(private)访问控制导致问题

swift 3 cocoa : use QuickLook to preview file in OS X

ios - 使用 UICollectionView 调整 UICollectionViewCell 的大小

objective-c - 在 iOS 中设置 UICollectionView

ios - 从其他类调用 CollectionView 中的下一张图像