Swift 4 - 对 alpha 和约束进行动画处理会影响其他组件的 alpha 值

Swift 4 - animating both alpha and constraint affects other components alpha value

我 运行 遇到了一个奇怪的情况,其中动画 UIImageView 的 alpha 会影响同样存在于同一视图中的 UIButton。

我的代码:

func handleArrowAnimation(_ arrowImage: UIImageView, _ arrowImageXCenterConstraint: NSLayoutConstraint) {

    arrowImageXCenterConstraint.constant = CGFloat(80)
    UIView.animate(withDuration: 0.7, delay: 0, options: [.curveEaseInOut, .repeat, .autoreverse], animations: {

        arrowImage.alpha = UIApplication.shared.userInterfaceLayoutDirection == .leftToRight ? 1 : 0.2
        arrowImage.superview!.layoutIfNeeded()

    }) { (completed) in

        arrowImageXCenterConstraint.constant = CGFloat(0)
        arrowImage.alpha = UIApplication.shared.userInterfaceLayoutDirection == .leftToRight ? 0.2 : 1
        arrowImage.superview!.layoutIfNeeded()
    }
}

结果:

我发现删除对 layoutIfNeeded() 的调用会阻止 UIButton alpha 更改,但当然它也会阻止箭头移动 - 所以它对我帮助不大。

UIButton 不是 arrowImage 的子视图,它们不共享相同的父视图(尽管它们的父视图共享相同的父视图)。

我在这里错过了什么?

谢谢!

很明显其他人遇到了这个问题,答案是确保在加载视图后启动动画,例如在 viewDidAppear() 方法中。

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear()
    handleArrowAnimation()
}

Here is the link to previous question. 目前似乎没有对这种奇怪行为的任何解释。