【问题标题】:Auto-Layout animation takes center as anchoring point自动布局动画以中心为锚点
【发布时间】:2018-01-02 18:52:30
【问题描述】:

我有一个基于约束的高度动画。但是,当动画开始时,它将视图的中心作为锚点而不是顶部。

我希望它动画为固定顶部并从底部收缩。

对不起,我的英语不是母语。

我的观点主播:

top: superview
left: superview
right: superview
bottom: nil

// current height constant 200
view.height.constant = 0

UIView.animation(withDuration: 0.2, animations: {
    view.layoutIfNeded()
}}

【问题讨论】:

    标签: ios swift3 autolayout ios-animations


    【解决方案1】:

    尝试代替你的观点:

    superView.layoutIfNeeded()
    

    这将布局子视图并且您的动画应该可以工作。

    所以替换

        // current height constant 200
    view.height.constant = 0
    
    UIView.animation(withDuration: 0.2, animations: {
        view.layoutIfNeded()
    }
    

        // current height constant 200
    yourView.height.constant = 0
    
    UIView.animation(withDuration: 0.2, animations: {
        yourViewSuperview.layoutIfNeded()
    }
    

    请参阅 layoutIfNeeded() 苹果文档:[1]https://developer.apple.com/documentation/uikit/uiview/1622507-layoutifneeded

    【讨论】:

    • 非常感谢!我使用的是 super.layoutIfNeeded 但 superview 解决了我的所有问题。
    【解决方案2】:

    更改代码

    // current height constant 200
    view.height.constant = 0
    
    UIView.animation(withDuration: 0.2, animations: {
        view.layoutIfNeded()
    }}
    

    UIView.animate(withDuration: 2.0, animations: {
    // current height constant 200
    view.height.constant = 0
    }}
    

    希望对你有帮助!

    【讨论】:

    • 它实际上不是动画,但现在没关系:)
    • 不符合需求,抱歉。在 animate func 中更改常量也无济于事
    猜你喜欢
    • 1970-01-01
    • 2017-03-18
    • 1970-01-01
    • 2014-05-10
    • 2019-11-14
    • 2016-01-28
    • 2023-03-26
    • 2015-11-15
    • 1970-01-01
    相关资源
    最近更新 更多