Skip to content

Instantly share code, notes, and snippets.

@swarut
Last active December 24, 2015 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swarut/6775341 to your computer and use it in GitHub Desktop.
Save swarut/6775341 to your computer and use it in GitHub Desktop.
Add inner shadow to uiview.
- (void)drawRect:(CGRect)rect {
[[UIImage imageNamed:@"top_bg_pattern.png"] drawAsPatternInRect:rect];
[self drawShadowsWithHeight:10 opacity:0.3 InRect:rect forContext:UIGraphicsGetCurrentContext()];
}
- (void)drawShadowsWithHeight:(CGFloat)shadowHeight opacity:(CGFloat)opacity InRect:(CGRect)rect forContext:(CGContextRef)context {
CGColorSpaceRef space = CGBitmapContextGetColorSpace(context);
CGFloat topComponents[8] = {0, 0, 0, opacity, 0, 0, 0, 0};
CGGradientRef topGradient = CGGradientCreateWithColorComponents(space, topComponents, nil, 2);
CGPoint finishTop = CGPointMake(rect.origin.x, rect.origin.y + shadowHeight);
CGContextDrawLinearGradient(context, topGradient, rect.origin, finishTop, kCGGradientDrawsAfterEndLocation);
CGFloat bottomComponents[8] = {0, 0, 0, 0, 0, 0, 0, opacity};
CGGradientRef bottomGradient = CGGradientCreateWithColorComponents(space, bottomComponents, nil, 2);
CGPoint startBottom = CGPointMake(rect.origin.x, rect.size.height - shadowHeight);
CGPoint finishBottom = CGPointMake(rect.origin.x, rect.size.height);
CGContextDrawLinearGradient(context, bottomGradient, startBottom, finishBottom, kCGGradientDrawsAfterEndLocation);
CGGradientRelease(topGradient);
CGGradientRelease(bottomGradient);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment