Created
June 29, 2010 00:02
-
-
Save preble/456577 to your computer and use it in GitHub Desktop.
BNRBlockView: Add custom-drawn views to the Cocoa view hierarchy without subclassing.
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
@class BNRBlockView; | |
typedef void(^BNRBlockViewDrawer)(BNRBlockView *view, NSRect dirtyRect); | |
@interface BNRBlockView : NSView { | |
BNRBlockViewDrawer drawBlock; | |
BOOL opaque; | |
} | |
+ (BNRBlockView *)viewWithFrame:(NSRect)frame | |
opaque:(BOOL)opaque | |
drawnUsingBlock:(BNRBlockViewDrawer)drawBlock; | |
@property (nonatomic, copy) BNRBlockViewDrawer drawBlock; | |
@property (nonatomic, assign) BOOL opaque; | |
@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
#import "BNRBlockView.h" | |
@implementation BNRBlockView | |
@synthesize drawBlock, opaque; | |
+ (BNRBlockView *)viewWithFrame:(NSRect)frame | |
opaque:(BOOL)opaque | |
drawnUsingBlock:(BNRBlockViewDrawer)theDrawBlock | |
{ | |
__typeof__(self) view = [[BNRBlockView alloc] initWithFrame:frame]; | |
[view setDrawBlock:theDrawBlock]; | |
[view setOpaque:opaque]; | |
return [view autorelease]; | |
} | |
- (void)dealloc | |
{ | |
[drawBlock release]; | |
[super dealloc]; | |
} | |
- (void)drawRect:(NSRect)dirtyRect { | |
if (drawBlock) | |
drawBlock(self, dirtyRect); | |
} | |
- (BOOL)isOpaque { | |
return opaque; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment