diff --git a/GlossyButtons/GlossyButton.h b/GlossyButtons/GlossyButton.h index d169bec..7583936 100644 --- a/GlossyButtons/GlossyButton.h +++ b/GlossyButtons/GlossyButton.h @@ -9,5 +9,6 @@ #import @interface GlossyButton : UIButton +@property(strong, nonatomic)UIColor *myColor; - (id)initWithFrame:(CGRect)frame withBackgroundColor:(UIColor*)backgroundColor; @end diff --git a/GlossyButtons/GlossyButton.m b/GlossyButtons/GlossyButton.m index 09d2212..1b2b739 100644 --- a/GlossyButtons/GlossyButton.m +++ b/GlossyButtons/GlossyButton.m @@ -16,6 +16,8 @@ - (id)initWithFrame:(CGRect)frame withBackgroundColor:(UIColor*)backgroundColor self = [super initWithFrame:frame]; if (self) { // Initialization code + //save our color so we can alter it upon a touch event + self.myColor = backgroundColor; [self makeButtonShiny:self withBackgroundColor:backgroundColor]; } return self; @@ -56,4 +58,55 @@ - (void)makeButtonShiny:(GlossyButton*)button withBackgroundColor:(UIColor*)back [button setBackgroundColor:backgroundColor]; } +//When button is touched, grab our existing color and make it 20% darker (or lighter if its black) +//We will return it to its original state when the touch is lifted and touchesEnded:withEvent: is called +-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event +{ + UIColor *newColor; + CGFloat red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0, white = 0.0; + + //Check if we're working with atleast iOS 5.0 + if([self.myColor respondsToSelector:@selector(getRed:green:blue:alpha:)]) { + [self.myColor getRed:&red green:&green blue:&blue alpha:&alpha]; + [self.myColor getWhite:&white alpha:&alpha]; + + //test if we're working with a grayscale, black or RGB color + if(!(red + green + blue) && white){ + //grayscale + newColor = [UIColor colorWithWhite:white - 0.2 alpha:alpha]; + } else if(!(red + green + blue) && !white) { + //black + newColor = [UIColor colorWithWhite:white + 0.2 alpha:alpha]; + } else{ + //RGB + newColor = [UIColor colorWithRed:red - 0.2 green:green - 0.2 blue:blue - 0.2 alpha:alpha]; + } + } else if(CGColorGetNumberOfComponents(self.myColor.CGColor) == 4) { + //for earlier than ios 5 + const CGFloat *components = CGColorGetComponents(self.myColor.CGColor); + red = components[0]; + green = components[1]; + blue = components[2]; + alpha = components[3]; + + newColor = [UIColor colorWithRed:red - 0.2 green:green - 0.2 blue:blue - 0.2 alpha:alpha]; + } else if(CGColorGetNumberOfComponents(self.myColor.CGColor) == 2){ + //if we have a non-RGB color + CGFloat hue; + CGFloat saturation; + CGFloat brightness; + [self.myColor getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha]; + + newColor = [UIColor colorWithHue:hue - 0.2 saturation:saturation - 0.2 brightness:brightness - 0.2 alpha:alpha]; + } + + self.backgroundColor = newColor; + +} + +-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event +{ + //Reset our button to its original color + self.backgroundColor = self.myColor; +} @end