gpt4 book ai didi

ios - UIView animateKeyframes 不适用于 NSLayoutConstraints

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

我正在尝试通过减少宽度约束来为 UIView 设置动画,但在使用 UIView.animateKeyFrames 时效果不佳。这是我的代码:

    UIView.animateKeyframes(withDuration: 2.0, delay: 0, options: [.calculationModeCubic], animations: {
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 1.0/2.0, animations: {
self.titleLabel.alpha = 0.0 // works well
})

self.titleSquareHeightConstraint?.constant = 20 // previously 170
UIView.addKeyframe(withRelativeStartTime: 1.0/2.0, relativeDuration: 1.0/2.0, animations: {
self.titleSquare.layoutIfNeeded() // instantaneously jump to 20
})

}, completion:nil)

titleSquare 的高度会瞬间从 170 跳到 20。 注意:titleSquare 是一个 UIView。此外,我曾经使用 UIView.animate 并且下面的代码工作得很好:

    self.titleSquareHeightConstraint?.constant = 20
UIView.animate(withDuration: 0.5,
delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 1,
options: .curveEaseIn,
animations: {

self.view.layoutIfNeeded() // Perfectly works

},completion: nil)

我的 UIView.animateKeyframes 方法有什么问题?似乎我不完全了解它是如何工作的?

谢谢

最佳答案

不是很明显或很直观,但是......

您想告诉 View layoutIfNeeded():

    UIView.animateKeyframes(withDuration: 2.0, delay: 0, options: [.calculationModeCubic], animations: {

UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 1.0/2.0, animations: {
self.titleLabel.alpha = 0.0 // works well
})

self.titleSquareHeightConstraint.constant = 20 // previously 170

UIView.addKeyframe(withRelativeStartTime: 1.0/2.0, relativeDuration: 1.0/2.0, animations: {

// tell self.view to layout, not the titleSquare view
self.view.layoutIfNeeded()

})

}, completion:nil)

关于ios - UIView animateKeyframes 不适用于 NSLayoutConstraints,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53339586/

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