Skip to content

Instantly share code, notes, and snippets.

@barbuza
Created April 18, 2013 11:58
Show Gist options
  • Save barbuza/5412187 to your computer and use it in GitHub Desktop.
Save barbuza/5412187 to your computer and use it in GitHub Desktop.
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
NSArray *gradientColors = @[(id)_startColor.CGColor, (id)_stopColor.CGColor];
NSArray *highlightedColors = @[(id)_stopColor.CGColor, (id)_startColor.CGColor];
CGGradientRef gradient = CGGradientCreateWithColors(colorspace, (__bridge CFArrayRef)gradientColors, NULL);
CGGradientRef highlightedGradient = CGGradientCreateWithColors(colorspace, (__bridge CFArrayRef)highlightedColors, NULL);
CGRect backgroundRect = rect;
UIBezierPath *backgroundPath = [UIBezierPath bezierPathWithRoundedRect:backgroundRect cornerRadius:3];
CGContextSaveGState(ctx);
[backgroundPath addClip];
CGGradientRef background = self.highlighted ? highlightedGradient : gradient;
CGContextDrawLinearGradient(ctx, background, CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect)),
CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect)), 0);
CGContextSetBlendMode(ctx, kCGBlendModeNormal);
UIImage *noiseImage = [UIImage imageNamed:@"noisy"];
[noiseImage drawAsPatternInRect:rect];
CGContextRestoreGState(ctx);
CGRect glowRect = CGRectInset(rect, 1, 1);
CGContextSaveGState(ctx);
CGContextAddRect(ctx, CGRectMake(0, 0, rect.size.width, 2.5));
CGContextClip(ctx);
if (self.highlighted) {
CGContextSetAlpha(ctx, 0.15);
} else {
CGContextSetAlpha(ctx, 0.3);
}
UIBezierPath *glowPath = [UIBezierPath bezierPathWithRoundedRect:glowRect cornerRadius:2.5];
[_innerGlowColor setStroke];
glowPath.lineWidth = 1.5;
[glowPath stroke];
CGContextRestoreGState(ctx);
UIBezierPath *borderPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:3];
[borderPath addClip];
[_borderColor setStroke];
borderPath.lineWidth = 1.5;
[borderPath stroke];
CGGradientRelease(gradient);
CGGradientRelease(highlightedGradient);
CGColorSpaceRelease(colorspace);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment