Skip to content

Instantly share code, notes, and snippets.

@hamrickdavid
Created August 7, 2011 22:48
Show Gist options
  • Save hamrickdavid/1130891 to your computer and use it in GitHub Desktop.
Save hamrickdavid/1130891 to your computer and use it in GitHub Desktop.
typedef void(^DrawView_DrawBlock)(UIView* v,CGContextRef context);
@interface DrawView : UIView
@property (nonatomic,copy) DrawableView_DrawBlock drawBlock;
@end
#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
DrawView* drawableView = [[[DrawView alloc] initWithFrame:CGRectMake(0,0,320,50)] autorelease];
drawableView.drawBlock = ^(UIView* v,CGContextRef context)
{
CGPoint startPoint = CGPointMake(0,v.bounds.size.height-1);
CGPoint endPoint = CGPointMake(v.bounds.size.width,v.bounds.size.height-1);
CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);
CGContextSetLineWidth(context, 1);
CGContextMoveToPoint(context, startPoint.x + 0.5, startPoint.y + 0.5);
CGContextAddLineToPoint(context, endPoint.x + 0.5, endPoint.y + 0.5);
CGContextStrokePath(context);
};
[self.view addSubview:drawableView];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment