ios - Swift 4 - 为 alpha 和约束设置动画会影响其他组件的 alpha 值

标签 ios swift animation alpha

我遇到了一种奇怪的情况,即对 UIImageView 的 alpha 进行动画处理会影响同样存在于同一 View 中的 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()
    }
}

结果:

enter image description here

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

UIButton 不是 arrowImage 的 subview ,它们不共享相同的父 View (尽管它们的父 View 共享相同的父 View )。

我在这里错过了什么?

谢谢!

最佳答案

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

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

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

关于ios - Swift 4 - 为 alpha 和约束设置动画会影响其他组件的 alpha 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46846581/

相关文章:

javascript - 如何在 React Native 中对 Flatlist 数组中的单个项目进行动画处理

ios - 使用观察者发出信号量?

ios - 解析服务器密码重置电子邮件错误ios

javascript - 动力学.js : Prevent draggable stage from going off-boundaries

swift - 是否可以在 Swift 中使用可选模式进行模式匹配?

ios - 使用数据将 PFFile 转换为 UIImage 并附加到数组

html - CSS3 无限旋转

ios - 如何在 Swift 中将字典传递给 Objective-C 方法

ios - 在 Swift 中,自定义 View 的 drawRect 未正确更新

ios - 使用 Jenkins 在模拟器上运行 iOS 应用程序