Core Animation is not working with "alpha" value Core Animation is not working with "alpha" value ios ios

Core Animation is not working with "alpha" value


[CABasicAnimation animationWithKeyPath:@"opacity"];

UIView exposes this as alpha where as CALayer exposes this as opacity.


For Swift:

let opacity = CABasicAnimation(keyPath: "opacity")opacity.fromValue = fromValueopacity.toValue = toValueopacity.duration = durationopacity.beginTime = CACurrentMediaTime() + beginTime  //If a delay is neededview.layer.add(opacity, forKey: nil)

If you want to keep the final alpha value, you have to set the current view controller as the delegate of the opacity animation:

opacity.delegate = self

And, in the delegate function animationDidStop, you should do:

extension ViewController: CAAnimationDelegate {    func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {           view.alpha = toValue    }}


@ohho answers the posted question. Mine will be a bit more generic. For a list what can and how be animated with CABasicAnimation please refer to Apple's documentation