Created
February 14, 2012 21:59
-
-
Save ksm/1830868 to your computer and use it in GitHub Desktop.
DrawView with a block for drawRect: an alternative to subclassing UIView for simple drawing operations
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
/* | |
Copied and pasted from David Hamrick's blog. | |
Source: http://www.davidhamrick.com/2011/08/07/using-blocks-for-drawing-to-avoid-subclasssing-in-objective-c.html | |
*/ | |
typedef void(^DrawView_DrawBlock)(UIView* v,CGContextRef context); | |
@interface DrawView : UIView | |
@property (nonatomic,copy) DrawableView_DrawBlock drawBlock; | |
@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
/* | |
Copied and pasted from David Hamrick's blog. | |
Source: http://www.davidhamrick.com/2011/08/07/using-blocks-for-drawing-to-avoid-subclasssing-in-objective-c.html | |
*/ | |
#import "DrawView.h" | |
@implementation DrawView | |
@synthesize drawBlock; | |
- (void)dealloc { | |
[drawBlock release], drawBlock = nil; | |
[super dealloc]; | |
} | |
- (void)drawRect:(CGRect)rect { | |
[super drawRect:rect]; | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
if(self.drawBlock) | |
self.drawBlock(self,context); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage: