2

我将尝试用一个简单的例子来解释我的问题。我有一个带有顶部约束和高度约束的 UIView。我试图动画增加高度约束。这里有两种方法。

    // case 1
    heightConstraint.constant += 100
    UIView.animate(withDuration: 0.8, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 25.0, options: [], animations: {
        self.view.layoutIfNeeded()
    }, completion: nil)

    // case 2
    heightConstraint.constant += 100
    let timingParams = UISpringTimingParameters(dampingRatio: 0.8, initialVelocity: CGVector(dx: 0, dy: 25.0))
    let animator = UIViewPropertyAnimator(duration: 0.8, timingParameters: timingParams)
    animator.addAnimations {
        self.view.layoutIfNeeded()
    }
    animator.startAnimation()

对于案例 1,动画按预期执行。然而,对于案例 2,动画忽略了我为 UIView 设置的固定顶部约束。它首先使用初始速度对视图执行平移,当速度达到 0 后,它开始相对于其中心缩放 UIView。

有人可以告诉我我错过了什么吗?

4

0 回答 0