Swift 带约束的动画

Swift animation with constraint

是否可以通过改变约束来制作UIView动画?

基本上,我想为具有 x、y、高度和宽度约束的 myv (UIView) 设置动画,使用:UIView.animateWithDuration(1.5) {} 通过更改旧约束。

是的,这是可能的。你可以这样做:

func AnimateHeight() {
    UIView.animateWithDuration(1.5, animations: {
         self.heightCons.constant = 250 // Some value
         self.view.layoutIfNeeded()    
    })
}

其中 self.heightCons 是对 UIView 高度限制的 IBOutlet

如果您不使用故事板,您可以查看

有关核心动画和自动布局的信息:Combining autolayout with core animation