[英]Why using self.view.layoutIfNeeded() in Animation cause App Freezing?
[英]What does `self.view.layoutIfNeeded()` do when changing constraints
所以我正在學習如何從屏幕外制作視圖動畫,以用作滑塊菜單。
class ViewController: UIViewController {
@IBOutlet weak var red: UIView!
@IBOutlet weak var redHConstraint: NSLayoutConstraint!
@IBAction func buttonShift(sender: AnyObject) {
self.view.layoutIfNeeded() // **HERE**
UIView.animateWithDuration(0.5) {
self.redHConstraint.constant = 0
self.view.layoutIfNeeded() // And **HERE**
}
}
}
我已經改編了如何使用Swift中的約束來動畫UIView的代碼?
1. self.view.layoutIfNeeded()
代碼的一部分是做什么的?
2.為什么在動畫之前和期間編碼2x?
注意:如果我注釋掉第一個self.view.layoutIfNeeded()
沒有任何變化,但如果我注釋掉第二個self.view.layoutIfNeeded()
該動作不再是動畫的,只會出現在新坐標中。
基本上調用self.view.layoutIfNeeded()
將強制self.view
及其子視圖的布局,如果某些更改為self.view
設置setNeedsLayout
。
setNeedsLayout
用於設置一個標志,每當調用layoutIfNeeded()
時,都會調用layoutSubviews
。 它決定了呼叫的“如果需要”方面。
如果你對UIView進行了更改,導致它變為setNeedsLayout
但是沒有調用layoutIfNeeded()
(因此不調用layoutSubviews),它將不會更新,因此可能會導致動畫出現問題。 如果您事先調用它,它將確保如果有更改需要更新布局,那么它將在動畫制作之前將其應用於您的視圖及其所有子視圖。
當然,在制作動畫時,您正在進行需要更新的更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.