Skip to content

Instantly share code, notes, and snippets.

@Jon889
Created September 18, 2013 06:01
Show Gist options
  • Save Jon889/6605097 to your computer and use it in GitHub Desktop.
Save Jon889/6605097 to your computer and use it in GitHub Desktop.
@interface UIImageView (highlighted)
@property (nonatomic, strong) NSMutableDictionary *highlightedKV;
@property (nonatomic, strong) NSMutableDictionary *unhighlightedKV;
@end
@implementation UIImageView (highlighted)
-(NSMutableDictionary *)highlightedKV {
id returnValue = objc_getAssociatedObject(self, "highlightedKVKey");
if (!returnValue) {
returnValue = [NSMutableDictionary dictionary];
[self setHighlightedKV:returnValue];
}
return returnValue;
}
-(void)setHighlightedKV:(NSMutableDictionary *)hkv {
objc_setAssociatedObject(self, "highlightedKVKey", hkv, OBJC_ASSOCIATION_RETAIN);
}
-(NSMutableDictionary *)unhighlightedKV {
id returnValue = objc_getAssociatedObject(self, "unhighlightedKVKey");
if (!returnValue) {
returnValue = [NSMutableDictionary dictionary];
[self setUnhighlightedKV:returnValue];
}
return returnValue;
}
-(void)setUnhighlightedKV:(NSMutableDictionary *)hkv {
objc_setAssociatedObject(self, "unhighlightedKVKey", hkv, OBJC_ASSOCIATION_RETAIN);
}
-(void)setHighlightedValue:(id)value forKey:(NSString *)key {
NSMutableDictionary *kv = [self highlightedKV];
kv[key] = value;
}
-(void)NEWsetHighlighted:(BOOL)highlighted {
NSMutableDictionary *kv = [self highlightedKV];
NSMutableDictionary *ukv = [self unhighlightedKV];
if ([self isHighlighted] == NO) {
for (NSString *key in kv) {
ukv[key] = [self valueForKey:key];
}
}
[self NEWsetHighlighted:highlighted];//thanks to swizzling this is not a loop.
NSMutableDictionary *loopDict = highlighted ? kv : ukv;
for (NSString *key in loopDict) {
[self setValue:loopDict[key] forKey:key];
}
}
+(void)load {
Method meth1 = class_getInstanceMethod([UIImageView class], @selector(setHighlighted:));
Method meth2 = class_getInstanceMethod([UIImageView class], @selector(NEWsetHighlighted:));
method_exchangeImplementations(meth1, meth2);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment