Skip to content

Instantly share code, notes, and snippets.

@wess
Created January 21, 2012 16:00
Show Gist options
  • Save wess/1653151 to your computer and use it in GitHub Desktop.
Save wess/1653151 to your computer and use it in GitHub Desktop.
added setBackground for event to UIButton
#import <UIKit/UIKit.h>
@interface UIButton(WCButton)
@property (nonatomic, retain) NSMutableDictionary *backgrounds;
- (void) setBackgroundColor:(UIColor *)bgColor forState:(UIControlState)state;
@end
#import "WCButton.h"
#import <objc/runtime.h>
@implementation UIButton(WCButton)
static char BG_PROPERTY_KEY;
@dynamic backgrounds;
- (void)setBackgrounds:(NSMutableDictionary *)backgrounds
{
objc_setAssociatedObject(self, &BG_PROPERTY_KEY, backgrounds, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSMutableDictionary *)backgrounds
{
return (NSMutableDictionary *)objc_getAssociatedObject(self, &BG_PROPERTY_KEY);
}
- (void) setBackgroundColor:(UIColor *)bgColor forState:(UIControlState)state
{
if([self backgrounds] == NULL)
{
NSMutableDictionary *tmpDict = [[NSMutableDictionary alloc] init];
[self setBackgrounds:tmpDict];
}
[[self backgrounds] setObject:bgColor forKey:[NSNumber numberWithInt:state]];
if(!self.backgroundColor)
self.backgroundColor = bgColor;
}
- (void)animateBackgroundToColor:(NSNumber *)key
{
UIColor *background = [[self backgrounds] objectForKey:key];
if(background)
{
[UIView animateWithDuration:0.1f animations:^{
self.backgroundColor = background;
}];
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
[self animateBackgroundToColor:[NSNumber numberWithInt:UIControlStateHighlighted]];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];
[self animateBackgroundToColor:[NSNumber numberWithInt:UIControlStateNormal]];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
[self animateBackgroundToColor:[NSNumber numberWithInt:UIControlStateNormal]];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment