重新加载布局更改旁边的UICollectionView/为其设置动画 [英] Reloading/animating a UICollectionView alongside a layout change

查看:25
本文介绍了重新加载布局更改旁边的UICollectionView/为其设置动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我正在更新集合视图的布局,以根据数据中存储的优先级更新大小。

它是这样工作的...

正如您从这段代码中看到的那样...

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

更新优先级将在单元格中显示/隐藏"ImageView"。它只是用于概念证明的标准UIView

但仅在布局更新完成后才显示/隐藏imageView

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

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

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

如果我像这样移动它...

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

然后动画更改为...

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

有没有一种方法可以以动画形式更改单元格的状态?理想情况下,我希望图像视图以与布局动画相同的速度显示动画。

如果我没有更改单元格中的内容,则这不是问题。但在某些情况下,我会想要更改单元格中的内容,而且我希望这两种方式都能顺利过渡。

推荐答案

确定...我在这里添加这个作为初步答案,但我认为我刚刚破解了它。

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

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

通过将priority信息放入布局属性中,我现在可以使图像视图在单元格移动和调整大小的同时进入视图。

轰!: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

这篇关于重新加载布局更改旁边的UICollectionView/为其设置动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆