Skip to content

Instantly share code, notes, and snippets.

@odrobnik
Created September 14, 2011 14:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odrobnik/1216647 to your computer and use it in GitHub Desktop.
Save odrobnik/1216647 to your computer and use it in GitHub Desktop.
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