Instantly share code, notes, and snippets.
Created
March 15, 2013 13:10
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save leeprobert/5169783 to your computer and use it in GitHub Desktop.
Simple UIButton subclass that has a nice embossed grey background drawn on to it. No bitmaps required.
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
// | |
// AGNSimpleButton.m | |
// iP2 | |
// | |
// Created by Lee Probert on 07/03/2013. | |
// | |
// | |
#import "AGNSimpleButton.h" | |
@implementation AGNSimpleButton | |
- (id)initWithFrame:(CGRect)frame | |
{ | |
self = [super initWithFrame:frame]; | |
if (self) { | |
// Initialization code | |
} | |
return self; | |
} | |
- (void)drawRect:(CGRect)rect | |
{ | |
//// General Declarations | |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
//// Color Declarations | |
UIColor* baseColor = [UIColor colorWithRed: 0.218 green: 0.24 blue: 0.268 alpha: 1]; | |
CGFloat baseColorRGBA[4]; | |
[baseColor getRed: &baseColorRGBA[0] green: &baseColorRGBA[1] blue: &baseColorRGBA[2] alpha: &baseColorRGBA[3]]; | |
UIColor* upperColor = [UIColor colorWithRed: (baseColorRGBA[0] * 0.8 + 0.2) green: (baseColorRGBA[1] * 0.8 + 0.2) blue: (baseColorRGBA[2] * 0.8 + 0.2) alpha: (baseColorRGBA[3] * 0.8 + 0.2)]; | |
UIColor* lowerColor = [UIColor colorWithRed: (baseColorRGBA[0] * 0.9) green: (baseColorRGBA[1] * 0.9) blue: (baseColorRGBA[2] * 0.9) alpha: (baseColorRGBA[3] * 0.9 + 0.1)]; | |
UIColor* lightUpColor = [UIColor colorWithRed: (baseColorRGBA[0] * 0.5 + 0.5) green: (baseColorRGBA[1] * 0.5 + 0.5) blue: (baseColorRGBA[2] * 0.5 + 0.5) alpha: (baseColorRGBA[3] * 0.5 + 0.5)]; | |
UIColor* lightDownColor = [UIColor colorWithRed: (baseColorRGBA[0] * 0.8) green: (baseColorRGBA[1] * 0.8) blue: (baseColorRGBA[2] * 0.8) alpha: (baseColorRGBA[3] * 0.8 + 0.2)]; | |
//// Gradient Declarations | |
NSArray* buttonGradientColors = [NSArray arrayWithObjects: | |
(id)upperColor.CGColor, | |
(id)lowerColor.CGColor, nil]; | |
CGFloat buttonGradientLocations[] = {0, 1}; | |
CGGradientRef buttonGradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)buttonGradientColors, buttonGradientLocations); | |
NSArray* overlayGradientColors = [NSArray arrayWithObjects: | |
(id)lightUpColor.CGColor, | |
(id)[UIColor clearColor].CGColor, nil]; | |
CGFloat overlayGradientLocations[] = {0, 1}; | |
CGGradientRef overlayGradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)overlayGradientColors, overlayGradientLocations); | |
NSArray* gradientColors = [NSArray arrayWithObjects: | |
(id)lightUpColor.CGColor, | |
(id)lightDownColor.CGColor, nil]; | |
CGFloat gradientLocations[] = {0, 1}; | |
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)gradientColors, gradientLocations); | |
//// Shadow Declarations | |
UIColor* buttonShadow = [UIColor blackColor]; | |
CGSize buttonShadowOffset = CGSizeMake(0.1, 1.1); | |
CGFloat buttonShadowBlurRadius = 2; | |
//// Frames | |
CGRect frame = rect; | |
//// Back Rectangle Drawing | |
CGRect backRectangleRect = CGRectMake(CGRectGetMinX(frame) + 1, CGRectGetMinY(frame) + 1, CGRectGetWidth(frame) - 2, CGRectGetHeight(frame) - 4); | |
UIBezierPath* backRectanglePath = [UIBezierPath bezierPathWithRoundedRect: backRectangleRect cornerRadius: 4]; | |
CGContextSaveGState(context); | |
CGContextSetShadowWithColor(context, buttonShadowOffset, buttonShadowBlurRadius, buttonShadow.CGColor); | |
CGContextBeginTransparencyLayer(context, NULL); | |
[backRectanglePath addClip]; | |
CGContextDrawLinearGradient(context, gradient, | |
CGPointMake(CGRectGetMidX(backRectangleRect), CGRectGetMinY(backRectangleRect)), | |
CGPointMake(CGRectGetMidX(backRectangleRect), CGRectGetMaxY(backRectangleRect)), | |
0); | |
CGContextEndTransparencyLayer(context); | |
CGContextRestoreGState(context); | |
//// Rounded Rectangle Drawing | |
CGRect roundedRectangleRect = CGRectMake(CGRectGetMinX(frame) + 2, CGRectGetMinY(frame) + 2, CGRectGetWidth(frame) - 4, CGRectGetHeight(frame) - 6); | |
UIBezierPath* roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect: roundedRectangleRect cornerRadius: 3]; | |
CGContextSaveGState(context); | |
[roundedRectanglePath addClip]; | |
CGContextDrawLinearGradient(context, buttonGradient, | |
CGPointMake(CGRectGetMidX(roundedRectangleRect), CGRectGetMinY(roundedRectangleRect)), | |
CGPointMake(CGRectGetMidX(roundedRectangleRect), CGRectGetMaxY(roundedRectangleRect)), | |
0); | |
CGContextRestoreGState(context); | |
//// Overlay Drawing | |
CGRect overlayRect = CGRectMake(CGRectGetMinX(frame) + 1, CGRectGetMinY(frame) + 1, CGRectGetWidth(frame) - 2, CGRectGetHeight(frame) - 4); | |
UIBezierPath* overlayPath = [UIBezierPath bezierPathWithRoundedRect: overlayRect cornerRadius: 4]; | |
CGContextSaveGState(context); | |
[overlayPath addClip]; | |
CGFloat overlayResizeRatio = MIN(CGRectGetWidth(overlayRect) / 120, CGRectGetHeight(overlayRect) / 23); | |
CGContextDrawRadialGradient(context, overlayGradient, | |
CGPointMake(CGRectGetMidX(overlayRect) + -53.28 * overlayResizeRatio, CGRectGetMidY(overlayRect) + -26.41 * overlayResizeRatio), 10 * overlayResizeRatio, | |
CGPointMake(CGRectGetMidX(overlayRect) + 0 * overlayResizeRatio, CGRectGetMidY(overlayRect) + -53.04 * overlayResizeRatio), 102.2 * overlayResizeRatio, | |
kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation); | |
CGContextRestoreGState(context); | |
//// Cleanup | |
CGGradientRelease(buttonGradient); | |
CGGradientRelease(overlayGradient); | |
CGGradientRelease(gradient); | |
CGColorSpaceRelease(colorSpace); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment