我认为每个帧都会调用一次view.layoutSubviews(),以避免计算布局的次数过多。这就是view.setNeedsLayout()存在的全部原因。这样,您可以多次调用view.setNeedsLayout(),包括每帧多次调用,但它只计算一次。这就是为什么还有第二个方法,view.layoutIfNeeded(),它可以立即计算布局。
这似乎不是这里发生的事情:
class MyViewController: UIViewController {
var time = Date().timeIntervalSince1970
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .cyan
Timer.scheduledTimer(withTimeInterval: 0.001, repeats: true) { (timer) in
self.view.setNeedsLayout()
print("hello")
}
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let newDate = Date().timeIntervalSince1970
print("Layout called at time: \(newDate - time)")
time = newDate
}}
这里发生的是“在时间调用布局...”每隔0.001次调用一次。
发布于 2020-03-17 23:42:27
它不是每帧一次,而是run-loop的每次迭代一次。由于您的应用程序实际上并没有做任何事情,所以这将与您的时间间隔一样快。
https://stackoverflow.com/questions/60725594
复制相似问题