0

我正在尝试为填充整个屏幕的 UIView 设置动画。我在这个 UIView 中有一个 UILabel,它有一些 NSLayoutConstraints。为 UIView 的宽度和高度约束设置动画时,UILabel 约束不会更新。我将标签宽度的约束设置为 UIView 的宽度锚点,并在 UIView.animate 块中调用 layoutIfNeeded()。这与标签的约束无关。

然后我决定为标签的约束设置动画,所以现在我将标签的宽度设置为与 UIView 相同的宽度,并将标签的顶部设置为 25。这可行,除了标签在动画开始,大约 75% 只是从右侧飞入?我不知道为什么会这样。任何帮助,将不胜感激。

动画代码:

guard let cell = collectionView.cellForItem(at: indexPath) as? NeuralNetLayerCollectionCell else {
            return
        }

        let expandingCellView = SlideOverView(frame: cell.bounds)
        expandingCellView.center = collectionView.convert(cell.center, to: self)
        expandingCellView.textLabel.text = cell.textLabel.text
        self.addSubview(expandingCellView)

        expandingCellView.widthConstraint.constant = self.frame.width
        expandingCellView.heightConstraint.constant = self.frame.height
        expandingCellView.labelWidthConstraint.constant = self.frame.width
        expandingCellView.labelTopConstraint.constant = 25

        UIView.animate(withDuration: 5, animations: {
            expandingCellView.center = self.center
            expandingCellView.layoutIfNeeded()
        })

和约束:

var widthConstraint: NSLayoutConstraint!
    var heightConstraint: NSLayoutConstraint!
    var labelWidthConstraint: NSLayoutConstraint!
    var labelTopConstraint: NSLayoutConstraint!

self.translatesAutoresizingMaskIntoConstraints = false

        widthConstraint = NSLayoutConstraint(item: self, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0, constant: self.bounds.width)
        heightConstraint = NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0, constant: self.bounds.height)

        self.backgroundColor = .white
        self.layer.cornerRadius = 25

        textLabel.textAlignment = .center
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        self.addSubview(textLabel)
        labelTopConstraint = textLabel.topAnchor.constraint(equalTo: self.topAnchor, constant: 25)
        let labelLeftConstraint = textLabel.leftAnchor.constraint(equalTo: self.leftAnchor)
        labelWidthConstraint = textLabel.widthAnchor.constraint(equalToConstant: self.bounds.width)
        let heightConstraintLabel = textLabel.heightAnchor.constraint(equalToConstant: 50)
        NSLayoutConstraint.activate([labelTopConstraint, labelLeftConstraint, labelWidthConstraint, heightConstraintLabel, widthConstraint, heightConstraint])

当前正在发生的事情的视频:https ://streamable.com/rk3ah 基本上我希望数字 2 随着视图的增长而向上移动,而不是消失然后从右侧飞入。这就是我删除所有其他代码时的样子,只有一个普通的 uiview 像另一个一样动画。标签仍然表现得很奇怪。我希望标签在整个动画过程中都可见。https://streamable.com/jaqu6

4

1 回答 1

0

collectionView 的单元格大小通常由collectionView:layout:sizeForItemAtIndexPath:( docs )控制

因此,如果在您的情况下是这样(没有更多上下文无法确定),您需要

  1. 为需要调整大小的单元格创建 invalidationContext ( doc )
  2. 将此 invalidationContext 传递给流布局 ( doc )
  3. 这个原因collectionView:layout:sizeForItemAtIndexPath:调用,你需要返回你的单元格的新大小
于 2020-03-11T14:18:08.383 回答