Search code examples
iosanimationcore-animationios15catransition

How to fix missing fade animation CATransition in ios15?


I have a bottom-up push animation of text. In iOS 14 and earlier, it worked with fade animation. Starting with iOS 15, the behavior has changed: the implicit fade disappeared. Any ideas on how to get it back?

It is necessary for the new layer to enter with opacity 0 to 1, and the old layer to go into opacity 0.

let pushAnimation: CATransition = {
  let animation = CATransition()
  animation.timingFunction = CAMediaTimingFunction(name: .easeIn)
  animation.type = .push
  animation.subtype = .fromTop
  animation.duration = 0.5
  return animation
}()
...

label.layer.add(pushAnimation, forKey: nil)
label.text = newText

Solution

  • You're perfectly correct that this behavior has changed in iOS 15. You might want to file a bug report with Apple. The implicit opacity change, however, was arguably always wrong, so they might reply that this now works as expected.

    A minimal solution here might be to add a fade animation to your transition:

        let ba = CABasicAnimation(keyPath: #keyPath(CALayer.opacity))
        ba.duration = 0.5
        ba.fromValue = 0
        label.layer.add(ba, forKey: nil)
    

    The fact is, however, that CATransition is extremely simple-minded, and if you want animation you can control, you should be using real animation. View transition animation, for example, will allow you replace one view with another while animating it in any way you wish, with full control over position animation, alpha animation and so forth.