简体   繁体   中英

Fading in a UIView shadow while resizing

I am attempting to scale my UIView at the same time as I fade in a shadow, using the following:

    myController.view.layer.shadowOffset = CGSizeMake(0, 3);
    myController.view.layer.shadowColor = [UIColor blackColor].CGColor;
    myController.view.layer.shadowOpacity = 0.0;
    myController.view.layer.shadowPath = [UIBezierPath bezierPathWithRect:myController.view.bounds].CGPath;

    [UIView animateWithDuration:0.3
                     animations:^{
                         //shrink the view
                         myController.view.transform = CGAffineTransformMakeScale(0.8, 0.8);

                         //fade in the shadow
                         myController.view.layer.shadowOpacity = 0.8;
                     }
                     completion:^(BOOL finished){
                          ...etc

The view resizes correctly but the shadow appears immediately instead of fading in.

Am I doing something wrong? I thought the shadowOpacity was animatable?

You have to use Core Animations to animate a views layer property:

#import <QuartzCore/CAAnimation.h>

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"shadowOpacity"];
animation.fromValue = [NSNumber numberWithFloat:1.0];
animation.toValue = [NSNumber numberWithFloat:0.8];
animation.duration = 1.0;
[myView.layer addAnimation:animation forKey:@"shadowOpacity"];
myView.layer.shadowOpacity = 0.0;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM