Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dropping a shadow
- (void)animationDidStop:(CABasicAnimation *)anim finished:(BOOL)flag
{
self.layer.shadowPath = (CGPathRef)anim.toValue;
}
- (void)updateShadowWithDuration:(NSTimeInterval)duration
{
CALayer *layer = self.layer;
if (_dropShadow)
{
layer.shadowColor = [UIColor blackColor].CGColor;
layer.shadowOffset = CGSizeMake(0,0);
layer.shadowOpacity = 0.9;
layer.shadowRadius = 10;
UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.bounds];
if (layer.shadowPath && duration>0)
{
NSLog(@"animate: %@", self);
// need animation
CABasicAnimation *theAnimation = [CABasicAnimation animationWithKeyPath:@"shadowPath"];
theAnimation.duration = duration;
theAnimation.fromValue = (id)layer.shadowPath;
theAnimation.toValue = (id)path.CGPath;
theAnimation.delegate = self;
theAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
theAnimation.removedOnCompletion = NO;
[layer addAnimation:theAnimation forKey:@"shadowPath"];
}
else
{
layer.shadowPath = path.CGPath;
}
}
else
{
// no shadow
layer.shadowOpacity = 0;
layer.shadowPath = NULL;
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment