ios - 约束动画不起作用

标签 ios animation swift3 constraints

我正在尝试在 2 个约束中设置优先级变化的动画,但我似乎无法让它发挥作用。我的动画代码是这样写的:

        UIView.animate(withDuration: 0.2, animations: {
        if self.cardHeaderBottomtoBodyTopConstraint.priority == UILayoutPriorityDefaultHigh {
            self.cardHeaderBottomtoBodyTopConstraint.priority = UILayoutPriorityDefaultLow
            self.cardHeaderBottomToBodyBottomConstraint.priority = UILayoutPriorityDefaultHigh
        } else {
            self.cardHeaderBottomtoBodyTopConstraint.priority = UILayoutPriorityDefaultHigh
            self.cardHeaderBottomToBodyBottomConstraint.priority = UILayoutPriorityDefaultLow
        }

        self.flightInformationBodyCard.layoutIfNeeded()
        self.flightInformationBodyCard.updateConstraints()
    }, completion: nil)

最佳答案

我所要做的就是将 layoutIfNeeded 方法更改为 superview,如下所示:

        if self.cardHeaderBottomtoBodyTopConstraint.priority == UILayoutPriorityDefaultHigh {
        self.cardHeaderBottomtoBodyTopConstraint.priority = UILayoutPriorityDefaultLow
        self.cardHeaderBottomToBodyBottomConstraint.priority = UILayoutPriorityDefaultHigh
    } else {
        self.cardHeaderBottomtoBodyTopConstraint.priority = UILayoutPriorityDefaultHigh
        self.cardHeaderBottomToBodyBottomConstraint.priority = UILayoutPriorityDefaultLow
    }

    //self.flightInformationBodyCard.updateConstraints()

    UIView.animate(withDuration: 0.2, animations: {

        self.flightInformationBodyCard.superview?.layoutIfNeeded()

    }, completion: nil)

关于ios - 约束动画不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45424028/

相关文章:

ios - 在导航栏左侧添加标题

css - 从底部旋转图像

安卓 Accordion / Accordion /折叠动画

jquery - Bootstrap 4 选项卡动画

ios - 从图层重新加载 tableView 时崩溃

ios - 在 Swift 3 中设置语言

swift - swift 3 中的可选值优化方式

ios - AFNetworking 2.0 缓存问题

ios - 有没有办法获取我在 iOS 设备上的 App Store 应用程序中获得的搜索结果? (如何获取 "App Store"的ipa文件?)

ios - 结合许多 UIViewPropertyAnimators