【问题标题】:How to make animation for height using transform?如何使用变换制作高度动画?
【发布时间】:2023-03-18 21:10:01
【问题描述】:

我有意见。例如 200 宽度和 100 高度。我想动画视图改变高度从 200 高度到 0 高度。我想用变换来做。怎么做?

【问题讨论】:

  • 有约束地去做

标签: ios swift animation transform


【解决方案1】:

你可以这样做:

UIView.animate(withDuration: 0.5) {
    yourView.transform.scaledBy(x: 1.0, y: 0.0)
}

如果要为约束设置动画,请使用以下命令:

myHeightConstraint.constant = 0
UIView.animate(withDuration: 0.5) {
    view.layoutIfNeeded()
}

【讨论】:

  • 我希望 myView 保持原位,只有高度发生了变化。它需要从下到上移动
  • 然后你需要像@Lu_ 说的那样为约束设置动画。我会更新我的答案
  • 可以通过变换实现吗?
  • transform 会将视图缩放到我猜的锚点
【解决方案2】:

我假设初始高度约束常数值为 200 然后:-

view.layoutIfNeeded() 
animatedViewHeightConstraint.constant = 0

UIView.animate(withDuration: 1.0, animations: { 
     self.view.layoutIfNeeded() 
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-15
    • 2019-02-20
    • 1970-01-01
    • 2014-02-07
    • 2013-03-18
    • 1970-01-01
    • 2022-12-16
    • 1970-01-01
    相关资源
    最近更新 更多