#import "MyView.h"
@implementation MyView
{
CGContextRef context;//UIGraphicsGetCurrentContext();
NSMutableArray *array ;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
/**/
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
[self drawTab];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if(array == nil){
array = [[NSMutableArray alloc]initWithCapacity:10];
}
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
[array addObject:[NSValue valueWithCGPoint:point]];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
[array addObject:[NSValue valueWithCGPoint:point]];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
[array addObject:[NSValue valueWithCGPoint:point]];
[self setNeedsDisplay];
}
-(void) drawTab {
context = UIGraphicsGetCurrentContext();
NSValue *value = nil;
for(int i=0;i<array.count;i++){
value = [array objectAtIndex:i];
CGPoint point ;
[value getValue:&point];
if(i==0){
CGContextBeginPath(context);
CGContextMoveToPoint(context,point.x ,point.y);
}else if(i == array.count-1){
CGContextAddLineToPoint(context, point.x, point.y);
CGContextDrawPath(context, kCGPathStroke);
}else {
CGContextAddLineToPoint(context, point.x, point.y);
}
}
}
@end