2016-09-14 16 views
0

をアニメーション化するときCenterX制約光栄ではない私は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) 

enter image description here

私はUILabelはを中心にしたいですアニメーションの始まりと終わり。 しかし、代わりにスーパービューの左上隅から始まるようです。 ここで何が間違っていますか?

答えて

2

問題は、この行とあった:

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

それは次のようになります。

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

centerXリレーショナルNSLayoutConstraintはスーパービューにある - ので、それはlayoutIfNeededは時に呼び出される必要スーパーです。