簡體   English   中英

使用 Swift 動畫視圖高度

[英]Animate view height with Swift

我的 ViewController 中有視圖sView 它的高度有約束——我為此約束創建了 IBOutlet—— sViewHeightConstraint 我想用 animation 降低 sView 的高度。

我創建了 function

UIView.animateWithDuration(5.5, animations: {
                self.sViewHeightConstraint.constant = 50
            })

視野高度正在改變,但我沒有看到任何 animation。我做錯了什么?

使用layoutIfNeeded()

 view.layoutIfNeeded() // force any pending operations to finish

 UIView.animateWithDuration(0.2, animations: { () -> Void in
    self.sViewHeightConstraint.constant = 50
    self.view.layoutIfNeeded()
})

迅捷3

view.layoutIfNeeded() 
sViewHeightConstraint.constant = 50

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

如果您的視圖更新但 animation 不工作,此解決方案對我有用。

UIView.animate(withDuration: 0.2, animations: { 
    self.sViewHeightConstraint.constant = 50
    self.sView.size = CGSize(width: self.sView.frame.width, height: 50)
    self.view.layoutIfNeeded() 
})

暫無
暫無

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

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