Skip to content

Instantly share code, notes, and snippets.

@clooth
Created August 23, 2013 14:45
Show Gist options
  • Save clooth/6320129 to your computer and use it in GitHub Desktop.
Save clooth/6320129 to your computer and use it in GitHub Desktop.
- (void)drawBevelArea
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect frame = self.frame;
CGRect bevelAreaRect = CGRectMake(0, 0, frame.size.width, frame.size.height);
UIBezierPath* bevelAreaPath = [UIBezierPath bezierPathWithRoundedRect:bevelAreaRect cornerRadius:_cornerRadius];
[_bevelBackgroundColor setFill];
[bevelAreaPath fill];
// Inner shadow
CGRect innerShadowRect = CGRectInset([bevelAreaPath bounds], -_bevelShadowBlurRadius, -_bevelShadowBlurRadius);
innerShadowRect = CGRectOffset(innerShadowRect, -_bevelShadowOffset.x, -_bevelShadowOffset.y);
innerShadowRect = CGRectInset(CGRectUnion(innerShadowRect, [bevelAreaPath bounds]), -1, -1);
// Negative path
UIBezierPath* bevelAreaNegativePath = [UIBezierPath bezierPathWithRect:innerShadowRect];
[bevelAreaNegativePath appendPath:bevelAreaPath];
bevelAreaNegativePath.usesEvenOddFillRule = YES;
// Draw shadow
CGContextSaveGState(context);
{
CGFloat xOffset = _bevelShadowOffset.x + round(innerShadowRect.size.width);
CGFloat yOffset = _bevelShadowOffset.y;
CGContextSetShadowWithColor(context,
CGSizeMake(xOffset + copysign(0.1, xOffset), yOffset + copysign(0.1, yOffset)),
_bevelShadowBlurRadius,
_bevelShadowColor.CGColor);
[bevelAreaPath addClip];
CGAffineTransform transform = CGAffineTransformMakeTranslation(-round(innerShadowRect.size.width), 0);
[bevelAreaNegativePath applyTransform:transform];
[[UIColor grayColor] setFill];
[bevelAreaNegativePath fill];
}
CGContextRestoreGState(context);
// Draw highlight bottom stroke
CGContextSaveGState(context);
NSArray* gradientColors = @[(id)[UIColor clearColor].CGColor,
(id)[UIColor clearColor].CGColor,
(id)[UIColor colorWithWhite:1.0 alpha:0.3].CGColor];
CGFloat gradientLocations[] = {0, 0.5, 1};
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)gradientColors, gradientLocations);
CGPathRef strokePath = CGPathCreateCopyByStrokingPath(bevelAreaPath.CGPath, NULL, 1.f, 0.f, 0.f, 0.f);
CGContextAddPath(context, strokePath);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(0, frame.size.height), 0);
CGContextRestoreGState(context);
CGGradientRelease(gradient);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment