我遇到了一种奇怪的情况,即对 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()
}
}
结果:
我发现删除对 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/