Skip to content

Instantly share code, notes, and snippets.

@odrobnik
Created April 26, 2012 12:03
Show Gist options
  • Save odrobnik/2499146 to your computer and use it in GitHub Desktop.
Save odrobnik/2499146 to your computer and use it in GitHub Desktop.
Convenience methods to add and animate a shadowPath
- (void)addShadowWithColor:(UIColor *)color alpha:(CGFloat)alpha radius:(CGFloat)radius offset:(CGSize)offset
{
self.layer.shadowOpacity = alpha;
self.layer.shadowRadius = radius;
self.layer.shadowOffset = offset;
if (color)
{
self.layer.shadowColor = [color CGColor];
}
// cannot have masking
self.layer.masksToBounds = NO;
}
- (void)updateShadowPathToBounds:(CGRect)bounds withDuration:(NSTimeInterval)duration
{
CGPathRef oldPath = self.layer.shadowPath;
CGPathRef newPath = CGPathCreateWithRect(bounds, NULL);
if (oldPath && duration>0)
{
CABasicAnimation *theAnimation = [CABasicAnimation animationWithKeyPath:@"shadowPath"];
theAnimation.duration = duration;
theAnimation.fromValue = (__bridge id)oldPath;
theAnimation.toValue = (__bridge id)newPath;
theAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[self.layer addAnimation:theAnimation forKey:@"shadowPath"];
}
self.layer.shadowPath = newPath;
CGPathRelease(newPath);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment