gpt4 book ai didi

ios - iOS 上的制图 : CenterX constraint not honored when animating

转载 作者:行者123 更新时间:2023-11-28 21:22:09 24 4
gpt4 key购买 nike

我正在尝试使用 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 在动画的开头和结尾居中。但是,它似乎是从 super View 的左上角开始的。我在这里做错了什么?

最佳答案

问题出在这一行:

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

应该是:

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

centerX 关系 NSLayoutConstraint 在父 View 上——因此它是需要调用 layoutIfNeeded 的父 View 。

关于ios - iOS 上的制图 : CenterX constraint not honored when animating,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39496546/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com