#在蓝懿学习iOS的日子#day21
今天主要是学习绘图的相关知识绘制单线,多条线、绘制图形
都要添加一个类,类名为DrawView
class是UIView,把XIB的VIew的Class改为类名进行关联
1、绘制单线
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.points = [NSMutableArray array];
}
return self;
}
-(void)awakeFromNib{
self.points = [NSMutableArray array];
NSLog(@"awakeFromNib");
}
//控件显示时会执行一次 当执行setNeedsDisplay
- (void)drawRect:(CGRect)rect {
//********* 绘制固定线的代码
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, 100, 100);
CGContextAddLineToPoint(context, 200, 200);
CGContextAddLineToPoint(context, 375, 0);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineWidth(context, 3);
//绘制 kCGPathStroke描边 kCGPathFill填充 kCGPathFillStroke 既填充右描边
CGContextDrawPath(context, kCGPathStroke);
// ********************手绘********************
CGContextRef c = UIGraphicsGetCurrentContext();
for (int i=0; i<self.points.count; i++) {
CGPoint p = [self.points[i] CGPointValue];
if (i==0) {
//第一次把画笔移动到某个位置
CGContextMoveToPoint(c, p.x, p.y);
}else{
CGContextAddLineToPoint(c, p.x, p.y);
}
}
CGContextSetStrokeColorWithColor(c, [UIColor redColor].CGColor);
//绘制
CGContextDrawPath(c, kCGPathStroke);
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *t = [touches anyObject];
CGPoint p = [t locationInView:self];
[self.points addObject:[NSValue valueWithCGPoint:p]];
//刷新显示
[self setNeedsDisplay];
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *t = [touches anyObject];
CGPoint p = [t locationInView:self];
[self.points addObject:[NSValue valueWithCGPoint:p]];
//刷新显示
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *t = [touches anyObject];
CGPoint p = [t locationInView:self];
[self.points addObject:[NSValue valueWithCGPoint:p]];
//刷新显示
[self setNeedsDisplay];
}
2、绘制多线
设置line类,可变数组记录所绘制的线,数组初始化
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.points = [NSMutableArray array];
}
return self;
}
-(void)awakeFromNib{
self.points = [NSMutableArray array];
NSLog(@"awakeFromNib");
}
//控件显示时会执行一次 当执行setNeedsDisplay
- (void)drawRect:(CGRect)rect {
//********* 绘制固定线的代码
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, 100, 100);
CGContextAddLineToPoint(context, 200, 200);
CGContextAddLineToPoint(context, 375, 0);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineWidth(context, 3);
//绘制 kCGPathStroke描边 kCGPathFill填充 kCGPathFillStroke 既填充右描边
CGContextDrawPath(context, kCGPathStroke);
// ********************手绘********************
CGContextRef c = UIGraphicsGetCurrentContext();
for (int i=0; i<self.points.count; i++) {
CGPoint p = [self.points[i] CGPointValue];
if (i==0) {
//第一次把画笔移动到某个位置
CGContextMoveToPoint(c, p.x, p.y);
}else{
CGContextAddLineToPoint(c, p.x, p.y);
}
}
CGContextSetStrokeColorWithColor(c, [UIColor redColor].CGColor);
//绘制
CGContextDrawPath(c, kCGPathStroke);
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *t = [touches anyObject];
CGPoint p = [t locationInView:self];
[self.points addObject:[NSValue valueWithCGPoint:p]];
//刷新显示
[self setNeedsDisplay];
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *t = [touches anyObject];
CGPoint p = [t locationInView:self];
[self.points addObject:[NSValue valueWithCGPoint:p]];
//刷新显示
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *t = [touches anyObject];
CGPoint p = [t locationInView:self];
[self.points addObject:[NSValue valueWithCGPoint:p]];
//刷新显示
[self setNeedsDisplay];
}
3、绘制图形
- (void)drawRect:(CGRect)rect {
CGContextRef c = UIGraphicsGetCurrentContext();
//矩形
// CGContextAddRect(c, CGRectMake(100, 100, 200, 100));
// CGContextSetStrokeColorWithColor(c, [UIColor redColor].CGColor);
// CGContextSetFillColorWithColor(c, [UIColor greenColor].CGColor);
// CGContextSetLineWidth(c, 5);
// CGContextDrawPath(c, kCGPathFillStroke);
//圆形
// CGContextAddEllipseInRect(c, CGRectMake(100, 100, 100, 100));
//
// CGContextDrawPath(c, kCGPathFillStroke);
//绘制扇形
CGContextMoveToPoint(c, 100, 100);
// 上下文 中心点 半径 开始 结束 顺逆时针
CGContextAddArc(c, 100, 100, 50, 0, 90*M_PI/180, 0);
CGContextDrawPath(c, kCGPathFill);
// 绘制图片
UIImage *image = [UIImage imageNamed:@"0.jpg"];
[image drawInRect:CGRectMake(200, 200, 100, 100)];
//绘制文本
NSDictionary *dic = @{NSFontAttributeName: [UIFont systemFontOfSize:30],NSForegroundColorAttributeName:[UIColor colorWithRed:135.0/255 green:181.0/255 blue:13.0/255 alpha:1]};
[@"hahahaha" drawInRect:CGRectMake(200, 300, 100, 100) withAttributes:dic];
}