iOS 绘图
-(id)initWithFrame:(CGRect)frame
{
self=[super initWithFrame:frame];
if (self) {
//使用self. 表示调用了set方法 retain了一次
self.lineArray=[NSMutableArray arrayWithCapacity:1];
}
return self;
}
-(void)drawRect:(CGRect)rect
{
//得到上下文,即配置信息
CGContextRef context = UIGraphicsGetCurrentContext();
//设置颜色
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
//画笔粗细
CGContextSetLineWidth(context, 2.0);
//拿到小数组
for (int i=0; i<[_lineArray count]; i++) {
NSMutableArray * pointArray = [_lineArray objectAtIndex:i];
//每一笔的每个点连成线
for (int j=0; j<(int)pointArray.count - 1; j++) {
NSValue * fistPointvalue=[pointArray objectAtIndex:j];//第一个点
NSValue * secondPointValue=[pointArray objectAtIndex:j+1];//的二个点
//转出CGPoint 类型对象
CGPoint firstPoint=[fistPointvalue CGPointValue];
CGPoint secondPoint=[secondPointValue CGPointValue];
//把笔触移动一个点
CGContextMoveToPoint(context, firstPoint.x , firstPoint.y);
//笔触和另一个点 连城一个路径
CGContextAddLineToPoint(context, secondPoint.x, secondPoint.y);
}
}
//执行绘制
CGContextStrokePath(context);
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSMutableArray *pointArray = [NSMutableArray arrayWithCapacity:1];
//内层数组
[_lineArray addObject:pointArray];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
NSValue *pointValue=[NSValue valueWithCGPoint:point];
NSLog(@"point=%@",NSStringFromCGPoint(point));
//大数组
NSMutableArray *pointArray=[_lineArray lastObject];
[pointArray addObject:pointValue];
//重绘
[self setNeedsDisplay];
}
-(void)dealloc
{
[_lineArray release];
[super dealloc];
}