The animation of a UIView
should start right after the view is added to the superview:
class myView: UIView {
override func didMoveToSuperview() {
super.didMoveToSuperview()
UIView.animate(
withDuration: duration,
delay: 0,
options: .curveLinear,
animations: {
CATransaction.begin()
CATransaction.setAnimationDuration(duration)
self.layer.colors = [color1, color2]
CATransaction.commit()
},
completion: nil
)
}
}
However, the result is that the animation is already over when the view becomes visible in the superview. UIView.animate
does not animate but sets self.layer.colors
immediately, maybe because the view is not yet visible when didMoveToSuperview
is called.
How can I make the animation start normally?