约束动画不起作用

时间:2017-07-31 19:38:04

标签: 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)

1 个答案:

答案 0 :(得分:2)

我所要做的就是将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)