iphoneobjective-ccocoa-touchuiviewquartz-graphics

How to animate layer shadowOpacity?


I have a view on which I've set the layerOpacity to 1.

    theView.layer.shadowOpacity = 1.0;

This looks fine when the view is farther down the screen. When I move this view up to be flush with another view that has a shadow, they don't look good. Is there a way I can animate the shadowOpacity on my layer to be 0? I tried using an animation block but it seems as if this property is not animatable.

alt text

EDIT: Request for code that doesn't work:

[UIView animateWithDuration:1.0 animations:^{
    splitView2.layer.shadowOpacity = 0;}
                 completion:NULL];

Solution

  • This will work properly:

    #import <QuartzCore/CAAnimation.h>
    
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"shadowOpacity"];
    anim.fromValue = [NSNumber numberWithFloat:1.0];
    anim.toValue = [NSNumber numberWithFloat:0.0];
    anim.duration = 1.0;
    [vv.layer addAnimation:anim forKey:@"shadowOpacity"];
    vv.layer.shadowOpacity = 0.0;
    

    For Swift 3.0:

     let animation = CABasicAnimation(keyPath: "shadowOpacity")
     animation.fromValue = layer.shadowOpacity
     animation.toValue = 0.0
     animation.duration = 1.0
     view.layer.add(animation, forKey: animation.keyPath)
     view.layer.shadowOpacity = 0.0