Created
May 17, 2012 11:29
-
-
Save joshdholtz/2718282 to your computer and use it in GitHub Desktop.
iOS - UIImageView+Block
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIImageView+Block.h | |
// | |
// Created by Josh Holtz on 5/17/12. | |
// Copyright (c) 2012 __MyCompanyName__. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface UIImageView (Block) | |
- (void) setOnTap:(void(^)())block; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIImageView+Block.m | |
// | |
// Created by Josh Holtz on 5/17/12. | |
// Copyright (c) 2012 __MyCompanyName__. All rights reserved. | |
// | |
#import "UIImageView+Block.h" | |
#import "/usr/include/objc/runtime.h" | |
@implementation UIImageView (Block) | |
static char overviewKey; | |
- (void) setOnTap:(void(^)())block { | |
[self setBlock:block]; | |
self.userInteractionEnabled = YES; | |
UITapGestureRecognizer *tapGesturePearls = | |
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap)]; | |
[self addGestureRecognizer:tapGesturePearls]; | |
} | |
- (void)setBlock:(void(^)())block { | |
objc_setAssociatedObject (self, &overviewKey,block,OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
} | |
- (void(^)())block { | |
return objc_getAssociatedObject(self, &overviewKey); | |
} | |
- (void)onTap { | |
void(^block)(); | |
block = [self block]; | |
block(); | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIImageView+Block.h | |
// | |
// Created by Josh Holtz on 5/17/12. | |
// Copyright (c) 2012 __MyCompanyName__. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface UIImageView (Block) | |
- (void) setOnTap:(void(^)())block; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIImageView+Block.m | |
// | |
// Created by Josh Holtz on 5/17/12. | |
// Copyright (c) 2012 __MyCompanyName__. All rights reserved. | |
// | |
#import "UIImageView+Block.h" | |
#import "/usr/include/objc/runtime.h" | |
@implementation UIImageView (Block) | |
static char overviewKey; | |
- (void) setOnTap:(void(^)())block { | |
[self setBlock:[block copy]]; | |
self.userInteractionEnabled = YES; | |
UITapGestureRecognizer *tapGesturePearls = | |
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap)]; | |
[self addGestureRecognizer:tapGesturePearls]; | |
} | |
- (void)setBlock:(void(^)())block { | |
objc_setAssociatedObject (self, &overviewKey,block,OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
} | |
- (void(^)())block { | |
return objc_getAssociatedObject(self, &overviewKey); | |
} | |
- (void)onTap { | |
void(^block)(); | |
block = [self block]; | |
block(); | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)someMethod { | |
[imageView setOnTap:^{ | |
NSLog(@"Do some stuff in this block"); | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment