// // MGMGradientButton.m // VoiceMob // // Created by Mr. Gecko on 10/1/10. // Copyright (c) 2010 Mr. Gecko's Media (James Coleman). All rights reserved. http://mrgeckosmedia.com/ // #import "MGMGradientButton.h" #import "MGMPath.h" #import "MGMVMAddons.h" @implementation MGMGradientButton - (void)awakeFromNib { buttonColor = [[self backgroundColor] retain]; buttonTouchColor = [[buttonColor colorWithDifference:-0.1] retain]; buttonDisabledColor = [[buttonColor colorWithDifference:0.1] retain]; [self setBackgroundColor:[UIColor clearColor]]; } - (void)dealloc { if (buttonColor!=nil) [buttonColor release]; if (buttonTouchColor!=nil) [buttonTouchColor release]; if (buttonDisabledColor!=nil) [buttonDisabledColor release]; [super dealloc]; } - (void)setTouching:(BOOL)isTouching { touching = isTouching; [self setNeedsDisplay]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesBegan:touches withEvent:event]; [self setTouching:YES]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesEnded:touches withEvent:event]; [self setTouching:NO]; } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesCancelled:touches withEvent:event]; [self setTouching:NO]; } - (void)setEnabled:(BOOL)isEnabled { [super setEnabled:isEnabled]; [self setNeedsDisplay]; } - (UIEdgeInsets)titleEdgeInsets { return UIEdgeInsetsMake(-2.0, 0.0, 0.0, 0.0); } - (void)drawRect:(CGRect)frameRect { UIColor *color = nil; if (![self isEnabled]) color = buttonDisabledColor; else if (touching) color = buttonTouchColor; else color = buttonColor; CGRect whitePathBounds = [self bounds]; CGRect pathBounds = whitePathBounds; whitePathBounds.size.width -= 2.0; whitePathBounds.origin.x += 1.0; whitePathBounds.size.height -= 2.0; whitePathBounds.origin.y += 0.7; MGMPath *whitePath = [MGMPath pathWithRoundedRect:whitePathBounds cornerRadius:12]; [[UIColor colorWithWhite:1.0 alpha:1.0] setStroke]; [whitePath setLineWidth:2.0]; [whitePath stroke]; pathBounds.size.width -= 1.0; pathBounds.origin.x += 0.5; pathBounds.size.height -= 2.0; pathBounds.origin.y += 0.5; MGMPath *path = [MGMPath pathWithRoundedRect:pathBounds cornerRadius:12]; [path setLineWidth:1.0]; [color setFill]; [[color colorWithDifference:-0.1] setStroke]; [path fill]; [path stroke]; CGRect gradientRect = pathBounds; gradientRect.size.width -= 1.0; gradientRect.origin.x += 0.5; gradientRect.size.height -= 0.5; gradientRect.origin.y += 0.5; CGFloat gradientRadius = 12.0; MGMPath *gradientPath = [MGMPath path]; float maxRadiusX = gradientRect.size.width / 2.0; float maxRadiusY = gradientRect.size.height / 2.0; gradientRadius = (gradientRadius