【发布时间】:2018-07-31 01:28:08
【问题描述】:
您好,我正在尝试了解视图更新周期
我几乎可以理解一些东西,但有些东西很神秘
我了解到动画块也会在更新周期触发
为了尝试理解动画块中的layoutIfNeeded,我编写了一些示例代码,如下所示
class MyView: UIView {
override func layoutSubviews() {
super.layoutSubviews()
print("Layout Subviews")
}
}
class ViewController: UIViewController {
@IBOutlet weak var blueHeight: NSLayoutConstraint!
@IBAction func heightPressed(_ sender: AnyObject) {
if self.blueHeight.constant == 25.0 {
self.blueHeight.constant = self.view.bounds.height - 100.0
} else {
self.blueHeight.constant = 25.0
}
self.view.setNeedsLayout()
UIView.animate(withDuration: 2.0, animations: {
print("animation block")
self.view.layoutIfNeeded() // ----- **
}) { (_) in
print("animation ended")
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
输出是这样的
"Layout Subviews"
"Layout Subviews" ---- because of initial settings
"Animation block"
"Layout Subviews"
"Animation ended"
但是当我将layoutIfNeeded 更改为setNeedsLayout 时,输出发生了变化
"Layout Subviews"
"Layout Subviews" ---- because of initial settings
"Animation block"
"Animation ended"
"Layout Subviews"
所以我知道动画块首先执行并查看更新,所以动画块在更新周期中比视图的更新具有更高的优先级
对吗?如果我错了,我想了解视图的更新周期和动画块之间的关系
【问题讨论】:
-
"当我将 layoutIfNeeded 更改为 setNeedsLayout 时,输出发生了更改" 如果您将
layoutIfNeeded更改为setNeedsLayout,您不妨将其完全删除;这是同一件事。layoutIfNeeded表示有一个动画——我们动画布局的行为。setNeedsLayout只是一个随机调用;这里没有动画。 -
感谢您的回复,但仍然感到困惑。
layoutIfNeeded直接触发layoutSubviews(不要等到更新周期)。但是动画块在更新周期中被触发,并且动画块有layoutIfNeeded。我对这种关系感到困惑
标签: ios uiview autolayout uiviewanimation