Can we animate "shadowOpacity" using UIView animation block?

We are pretty much used to the UIView animation block for properties like frame and transforms.

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations

Lets say there is a UIView *userView and you would like to show animation of shadow turned on or off.

    [UIView animateWithDuration:01 animations:^{
        if(showShadow){
            self.userView.layer.shadowOpacity=0.5;
        }else{
            self.userView.layer.shadowOpacity=0.0;
        }
    }];

The above code is not going to work. UIView animates  frame, bounds, center, tranform, alpha, background Color and contentStretch.i.e simple animation.

The View Programming guide - Animations url gives more details on it. 

It does not animate layer properties. So , we will have to use CABasicAnimation. The following code will work perfectly fine.

   float start=0.5,end=0.0;//example
    CABasicAnimation *animation  = [CABasicAnimation animation];
    animation.fromValue= @(start);
    animation.toValue= @(end);
    animation.duration = 1.0;
    [self.userView.layer addAnimation:animation forKey:@"shadowOpacity"];
    self.userView.layer.shadowOpacity=end;









Comments

Post a Comment

Popular posts from this blog

hitTest on iOS to identify the view being hit

CMTimeMakeWithSeconds explained

How to set Custom Section Header in UITableView for ios sdk version greater than 6