ios 使用 Swift 动画视图高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36953508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:59:19  来源:igfitidea点击:

Animate view height with Swift

iosxcodeswiftautolayout

提问by moonvader

I have view sViewinside my ViewController. It height has constraint - I created IBOutlet for this constraint - sViewHeightConstraint. I want to decrease height of sView with animation.

我的 ViewController 中有视图sView。它的高度有约束 - 我为此约束创建了 IBOutlet - sViewHeightConstraint。我想用动画降低 sView 的高度。

I created 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?

视图高度在变化,但我没有看到任何动画。我做错了什么?

回答by EI Captain v2.0

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() 
})