简体   繁体   English

使用 Swift 动画视图高度

[英]Animate view height with Swift

I have view sView inside my ViewController.我的 ViewController 中有视图sView It height has constraint - I created IBOutlet for this constraint - sViewHeightConstraint .它的高度有约束——我为此约束创建了 IBOutlet—— sViewHeightConstraint I want to decrease height of sView with animation.我想用 animation 降低 sView 的高度。

I created function我创建了 function

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

Height of view is changing but i don't see any animation. What I am doing wrong?视野高度正在改变,但我没有看到任何 animation。我做错了什么?

use layoutIfNeeded() 使用layoutIfNeeded()

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

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

swift 3 迅捷3

view.layoutIfNeeded() 
sViewHeightConstraint.constant = 50

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

In case your view updates but animation isn't working, this solution works for me.如果您的视图更新但 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