簡體   English   中英

iOS Animate高度限制問題

[英]iOS Animate height constraint issue

更改高度限制后,視圖的動畫出現問題。 在屏幕截圖中,您可以看到它的初始值為120.0。 在此處輸入圖片說明 動畫可以工作,但是第二個視圖(藍色視圖)中的約束更新直接發生,而不是在動畫過程中發生。 這意味着第二個視圖直接跳到頂部。 通過以下代碼,我將為高度約束的變化設置動畫:

UIView.animate(withDuration: 3.0, animations: {
    self.heightConstraint?.constant = 0.0
    self.myLabel.alpha = 0.0
    self.layoutIfNeeded()
})

有人知道為什么嗎?

self.heightConstraint?.constant = 0.0
self.myLabel.alpha = 0.0
UIView.animate(withDuration: 3.0, animations: {
    self.layoutIfNeeded()
})

應該是這樣

為了動畫化約束更改,您需要編寫如下代碼才能工作。

self.heightConstraint?.constant = 0.0
self.myLabel.alpha = 0.0

UIView.animate(withDuration: 5) {
    self.layoutIfNeeded()
}

您需要在更新約束常量之前和之后調用self.layoutIfNeeded() 將您的代碼更改為:

self.layoutIfNeeded()

UIView.animate(withDuration: 3.0, animations: {
    self.heightConstraint?.constant = 0.0
    self.myLabel.alpha = 0.0
    self.layoutIfNeeded()
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM