Last active
December 24, 2015 09:09
-
-
Save swarut/6775341 to your computer and use it in GitHub Desktop.
Add inner shadow to uiview.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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