【问题标题】:Animate view height with Swift使用 Swift 动画视图高度
【发布时间】:2016-08-25 12:14:59
【问题描述】:

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

我创建了函数

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

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

【问题讨论】:

    标签: ios xcode swift autolayout


    【解决方案1】:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多