Skip to content

Commit

Permalink
Merge pull request #1 from Jpro714/touch-effect
Browse files Browse the repository at this point in the history
added button pressed effect on touch
  • Loading branch information
ijason committed Jun 11, 2013
2 parents a901d9e + 992772a commit 04be1e2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions GlossyButtons/GlossyButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
#import <UIKit/UIKit.h>

@interface GlossyButton : UIButton
@property(strong, nonatomic)UIColor *myColor;
- (id)initWithFrame:(CGRect)frame withBackgroundColor:(UIColor*)backgroundColor;
@end
53 changes: 53 additions & 0 deletions GlossyButtons/GlossyButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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

0 comments on commit 04be1e2

Please sign in to comment.