Created
June 13, 2012 09:52
-
-
Save jamztang/2923136 to your computer and use it in GitHub Desktop.
UIBlocksView - Adding blocks to UIViews
This file contains 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
// ARC based | |
#import <UIKit/UIKit.h> | |
typedef void(^UIDrawRectBlock)(CGRect rect); | |
typedef void(^UILayoutSubviewBlock)(void); | |
@interface UIBlocksView : UIView | |
- (void)onDrawRectHandler:(UIDrawRectBlock)block; | |
- (void)onLayoutSubviewsHandler:(UILayoutSubviewBlock)block; | |
@end |
This file contains 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
#import "UIBlocksView.h" | |
@interface UIBlocksView () | |
@property (nonatomic, copy) UIDrawRectBlock drawRectBlock; | |
@property (nonatomic, copy) UILayoutSubviewBlock layoutSubviewsBlock; | |
@end | |
@implementation UIBlocksView | |
@synthesize drawRectBlock, layoutSubviewsBlock; | |
- (id)initWithFrame:(CGRect)frame { | |
self = [super initWithFrame:frame]; | |
if (self) { | |
self.drawRectBlock = nil; | |
self.layoutSubviewsBlock = nil; | |
} | |
return self; | |
} | |
- (void)drawRect:(CGRect)rect { | |
if (self.drawRectBlock) { | |
self.drawRectBlock(rect); | |
} | |
} | |
- (void)layoutSubviews { | |
[super layoutSubviews]; | |
if (self.layoutSubviewsBlock) { | |
self.layoutSubviewsBlock(); | |
} | |
} | |
- (void)onLayoutSubviewsHandler:(UILayoutSubviewBlock)block { | |
self.layoutSubviewsBlock = block; | |
} | |
- (void)onDrawRectHandler:(UIDrawRectBlock)block { | |
self.drawRectBlock = block; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment