【问题标题】:Animate scaling UIView with nested UIViews使用嵌套 UIView 动画缩放 UIView
【发布时间】:2020-03-11 13:58:09
【问题描述】:

我正在尝试为填充整个屏幕的 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

【问题讨论】:

  • 可以添加视频样本吗?预期和你目前得到什么??
  • 是的,我现在就这样做。
  • 好的,我现在添加了一个视频。
  • 这是带有文本 2 的标签?
  • 它是扩展 uiview 中的标签,我希望这个标签按照上面的说明进行动画处理。

标签: swift uiview autolayout uikit constraints


【解决方案1】:

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

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

  1. 为需要调整大小的单元格创建 invalidationContext (doc)
  2. 将此 invalidationContext 传递给流布局 (doc)
  3. 这会导致collectionView:layout:sizeForItemAtIndexPath: 调用,您需要返回新的单元格大小

【讨论】:

  • 我在单元格顶部添加一个视图,其大小和位置与单元格相同,然后为新视图的缩放设置动画。
猜你喜欢
  • 2011-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多