Skip to content

Instantly share code, notes, and snippets.

@darkseed
Created May 11, 2011 22:13
Show Gist options
  • Save darkseed/967506 to your computer and use it in GitHub Desktop.
Save darkseed/967506 to your computer and use it in GitHub Desktop.
Curved shadow below an image
#import <QuartzCore/QuartzCore.h>
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:@"photo.png"];
UIView *photoView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height)];
photoView.center = CGPointMake(self.view.center.x, self.view.center.y - 10);
photoView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleBottomMargin;
photoView.layer.contents = (id)image.CGImage;
photoView.layer.borderColor = [UIColor colorWithWhite:1.0 alpha:1.0].CGColor;
photoView.layer.borderWidth = 5.0;
photoView.layer.shadowOffset = CGSizeMake(0, 3);
photoView.layer.shadowOpacity = 0.7;
photoView.layer.shouldRasterize = YES;
NSLog(@"Photo view frame: %@", NSStringFromCGRect(photoView.frame));
NSLog(@"Photo view center: %@", NSStringFromCGPoint(photoView.center));
UIBezierPath *path = [UIBezierPath bezierPathWithCurvedShadowForRect:photoView.bounds];
photoView.layer.shadowPath = path.CGPath;
[self.view addSubview:photoView];
[photoView release];
}
+ (UIBezierPath *)bezierPathWithCurvedShadowForRect:(CGRect)rect
{
UIBezierPath *path = [UIBezierPath bezierPath];
CGPoint topLeft = rect.origin;
CGPoint bottomLeft = CGPointMake(0.0, CGRectGetHeight(rect) + offset);
CGPoint bottomMiddle = CGPointMake(CGRectGetWidth(rect)/2, CGRectGetHeight(rect) - curve);
CGPoint bottomRight = CGPointMake(CGRectGetWidth(rect), CGRectGetHeight(rect) + offset);
CGPoint topRight = CGPointMake(CGRectGetWidth(rect), 0.0);
NSLog(@"rect: %@", NSStringFromCGRect(rect));
NSLog(@"topLeft: %@", NSStringFromCGPoint(topLeft));
NSLog(@"bottomLeft: %@", NSStringFromCGPoint(bottomLeft));
NSLog(@"bottomMiddle: %@", NSStringFromCGPoint(bottomMiddle));
NSLog(@"bottomRight: %@", NSStringFromCGPoint(bottomRight));
NSLog(@"topRight: %@", NSStringFromCGPoint(topRight));
[path moveToPoint:topLeft];
[path addLineToPoint:bottomLeft];
[path addQuadCurveToPoint:bottomRight controlPoint:bottomMiddle];
[path addLineToPoint:topRight];
[path addLineToPoint:topLeft];
[path closePath];
return path;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment