Skip to content

Instantly share code, notes, and snippets.

@joshdholtz
Created May 17, 2012 11:29
Show Gist options
  • Save joshdholtz/2718282 to your computer and use it in GitHub Desktop.
Save joshdholtz/2718282 to your computer and use it in GitHub Desktop.
iOS - UIImageView+Block
//
// 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
//
// 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
//
// 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
//
// 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
- (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