0

我正在尝试使用Cartography为 UILabel 设置动画 并使用以下代码:

    let group = ConstraintGroup()

    constrain(alertLabel, replace: group) { alertLabel in
        alertLabel.centerX  == alertLabel.superview!.centerX
        alertLabel.width == alertLabel.superview!.width * Constants.alertLabelWidthMultiplier
        alertLabel.bottom == alertLabel.superview!.top + 0
    }

    constrain(alertLabel, replace: group) { alertLabel in
        alertLabel.centerX  == alertLabel.superview!.centerX
        alertLabel.width == alertLabel.superview!.width * Constants.alertLabelWidthMultiplier
        alertLabel.bottom == alertLabel.superview!.top + 60
    }

    UIView.animateWithDuration(0.5, animations: alertLabel.layoutIfNeeded)

在此处输入图像描述

我希望我的 UILabel 在动画的开头和结尾居中。但是,它似乎从超级视图的左上角开始。我在这里做错了什么?

4

1 回答 1

2

问题出在这一行:

UIView.animateWithDuration(0.5, animations: alertLabel.layoutIfNeeded)

它应该是:

UIView.animateWithDuration(0.5, animations: alertLabel.superview!.layoutIfNeeded)

centerX 关系 NSLayoutConstraint 在超级视图上 - 因此它是需要 layoutIfNeeded 调用的超级视图。

于 2016-09-14T18:59:26.397 回答