- (void)drawRect:(CGRect)rect
{
//该方法提供给我们在视图显示之前自己绘制一些东西
//不需要创建画板
//获取当前的画板
CGContextRef context = UIGraphicsGetCurrentContext();
//设置画笔的颜色
// CGContextSetStrokeColorWithColor(context, <#CGColorRef color#>)
CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);
//设置线条的宽度
CGContextSetLineWidth(context, 10);
//线条的样式
CGContextSetLineCap(context, kCGLineCapButt);
//把画笔移到某个点
if (allLinesArr.count == 0)
{
return;
}
for (NSMutableArray *smallArr in allLinesArr)
{
CGPoint firstPoint = [smallArr[0] CGPointValue];
CGContextMoveToPoint(context, firstPoint.x, firstPoint.y);
//跟另一个点连成一条线
for (int i = 1; i < smallArr.count; i++)
{
CGPoint nextPoint = [smallArr[i] CGPointValue];
CGContextAddLineToPoint(context, nextPoint.x, nextPoint.y);
}
}
//把画板上刚才描的路径画出来
//画线
CGContextStrokePath(context);
//画圆
//CGContextStrokeEllipseInRect(context, CGRectMake(150, 200, 200, 200));
//画矩形
// CGContextStrokeRect(context, CGRectMake(80, 220, 250, 250));
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
pointArr = [[NSMutableArray alloc]init];
UITouch *touch = [touches anyObject];
CGPoint beginPoint = [touch locationInView:self];
[pointArr addObject:[NSValue valueWithCGPoint:beginPoint]];
[allLinesArr addObject:pointArr];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint movePoint = [touch locationInView:self];
[pointArr addObject:[NSValue valueWithCGPoint:movePoint]];
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
iOS绘图
于 2015-10-23 08:45:52 首次发布