CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor cyanColor].CGColor);
CGContextSetLineWidth(context, 5);
for (int i = 0 ; i < [_lineArray count] ; i ++ ) {
//取出一条线(点得数组)
NSMutableArray * pointArr = [_lineArray objectAtIndex:i];
if ([pointArr count] > 0 ) {
for (int j = 0 ; j < [pointArr count] -1; j ++ ) {
NSValue * a = [pointArr objectAtIndex:j ];
CGPoint pointA = [a CGPointValue];
NSValue * b = [pointArr objectAtIndex:j + 1];
CGPoint pointB = [b CGPointValue];
CGContextMoveToPoint(context, pointA.x, pointA.y);
CGContextAddLineToPoint(context, pointB.x, pointB.y);
}
}
}
CGContextStrokePath(context);
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//每次点击屏幕的时候,创建一条线的数组
NSMutableArray * points = [NSMutableArray array];
//把创建好的线添加到线的数组里面
[self.lineArray addObject:points];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//获得最新的线
NSMutableArray * pointArr = [self.lineArray lastObject];
//获得当前经过的点
//获得UITouch
UITouch * touch = [touches anyObject];
//获得touch中存储的点
CGPoint point = [touch locationInView:self];
//把CGPoint 转换成对象(NSValue)
NSValue * value= [NSValue valueWithCGPoint:point];
//将接触获得的点添加到当前正在绘制的线中
[pointArr addObject:value];
//强制UIView调用drawRect方法
[self setNeedsDisplay];
}
//context包含UIView需要的各种信息
// CGContextRef context = UIGraphicsGetCurrentContext();
// //1、设置线的颜色
// CGContextSetStrokeColorWithColor(context, [UIColor cyanColor].CGColor);
// //2、设置线的粗细
// CGContextSetLineWidth(context, 6);
// //3、设置线段的起点
// CGContextMoveToPoint(context, 20, 20);
// //4、设置线段的终点
//// CGContextAddLineToPoint(context, 220, 220);
//
// //绘制椭圆
//// CGContextAddEllipseInRect(context, CGRectMake(20, 20, 200, 200));
// //绘制方形
//// CGContextAddRect(context, CGRectMake(20, 20, 200, 200));
// //绘制曲线 参数: context 曲线参考点坐标,结束点坐标 ( 起点为之前停止的地方)
// CGContextAddQuadCurveToPoint(context, 0, 200, 320, 220);
//
// //5、绘制设置好的线
// CGContextStrokePath(context);
// CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);
// CGContextSetLineWidth(context, 5.2f);
//
// UIColor * aColor2 = [UIColor cyanColor];//天蓝色
// CGContextSetFillColorWithColor(context, aColor2.CGColor);//填充颜色
// CGContextAddArc(context, 150, 50, 30, 50, 60, 0);//填充圆
// CGContextFillPath(context);
//
// CGContextAddArc(context, 40, 280, 30, 50, 60, 0);//空心圆
// CGContextStrokePath(context);
//
//
// UIColor * aColor = [UIColor cyanColor];//天蓝色
// CGContextSetFillColorWithColor(context, aColor.CGColor);//填充颜色
// CGContextFillRect(context,CGRectMake(10, 180, 150, 50));//填充框
//
// CGContextStrokeRect(context,CGRectMake(10, 120, 150, 50));//空心框
//
//
// UIColor * aColor1 = [UIColor grayColor];//灰色
// CGContextSetFillColorWithColor(context, aColor1.CGColor);//填充颜色
// CGContextAddEllipseInRect(context, CGRectMake(90, 280, 60, 30)); //椭圆
// CGContextDrawPath(context, kCGPathFillStroke);
//
//
// //画三角形
// CGPoint sPoints[3];//坐标点
// sPoints[0] =CGPointMake(165, 285);//坐标1
// sPoints[1] =CGPointMake(195, 285);//坐标2
// sPoints[2] =CGPointMake(195, 225);//坐标3
// CGContextAddLines(context, sPoints, 3);//添加线
// CGContextClosePath(context);//封起来
// CGContextDrawPath(context, kCGPathFillStroke); //根据坐标绘制路径
// CGContextStrokePath(context);//开始绘制
//上午练习
//-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//{
// [self setBackgroundColor:[UIColor yellowColor]];
// NSLog(@"%@",touches);
// UITouch * touch = [touches anyObject];
// NSLog(@"%d",touch.tapCount);
//}
//-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
//{
// [self setBackgroundColor:[UIColor cyanColor]];
//}
//-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
//{
// [self setBackgroundColor:[UIColor blueColor]];
//}
//-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
//{
// [self setBackgroundColor:[UIColor greenColor]];
// NSLog(@"lallalalalala");
//}
//-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
//{
// [self setBackgroundColor:[UIColor purpleColor]];
//}
[self setBackgroundColor:[UIColor yellowColor]];
NSLog(@"%@",touches);
UITouch * touch = [touches anyObject];
NSLog(@"%d",touch.tapCount);