文章搬运来源:blog.csdn.net/Calvin_zhou…面试
做者:PGzxcmarkdown
对iOS开发感兴趣,能够看一下做者的iOS交流群:812157648,你们能够在里面吹水、交流相关方面的知识,群里还有我整理的有关于面试的一些资料,欢迎你们加群,你们一块儿开车oop
本文介绍基本图形的绘制:spa
- (void)drawRect:(CGRect)rect {
//1.获取上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();
//2.拼接路径
UIBezierPath *path=[UIBezierPath bezierPath];
CGPoint startP=CGPointMake(10, 10);
[path moveToPoint:startP];
[path addLineToPoint:CGPointMake(125, 125)];
[path addLineToPoint:CGPointMake(240, 10)];
[path closePath];
//[path addLineToPoint:startP];
//3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
[[UIColor blueColor]setFill];
[[UIColor redColor]setStroke];
CGContextSetLineWidth(ctx, 15);
//4.渲染上下文
//CGContextStrokePath(ctx);
//CGContextFillPath(ctx);
CGContextDrawPath(ctx,kCGPathFillStroke);
}
复制代码
- (UILabel *)label{
if (_label==nil) {
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 250, 100)];
label.text=@"s";
label.textColor=[UIColor yellowColor];
label.font=[UIFont systemFontOfSize:60];
label.textAlignment=NSTextAlignmentCenter;
[self addSubview:label];
}
return _label;
}
- (void)awakeFromNib
{
self.label;
}
复制代码
- (void)drawRect:(CGRect)rect {
//1.获取上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();
//2.拼接路径
UIBezierPath *paht=[UIBezierPath bezierPathWithRect:CGRectMake(10, 10, 200, 200)];
//3.把路径添加到上下文
CGContextAddPath(ctx, paht.CGPath);
//4.渲染上下文
CGContextStrokePath(ctx);
}
复制代码
- (void)drawRect:(CGRect)rect {
//1.获取上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();
//2.拼接路径
UIBezierPath *path=[UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10, 200, 200)];
//3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
//4.渲染上下文
CGContextStrokePath(ctx);
}
复制代码
- (void)drawRect:(CGRect)rect {
//1.获取上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();
//2.拼接路径
UIBezierPath *paht=[UIBezierPath bezierPathWithRect:CGRectMake(10, 10, 200, 200)];
paht=[UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 10, 200, 200) cornerRadius:100];
//3.把路径添加到上下文
CGContextAddPath(ctx, paht.CGPath);
//4.渲染上下文
CGContextStrokePath(ctx);
}
复制代码
- (void)drawRect:(CGRect)rect {
//1.获取上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();
//2.拼接路径
UIBezierPath *path=[UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10, 200, 100)];
//3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
//4.渲染上下文
CGContextStrokePath(ctx);
}
复制代码
- (void)drawRect:(CGRect)rect {
//1.获取上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();
//2.拼接路径
CGPoint center=CGPointMake(125, 125);
CGFloat radius=100;
CGFloat startA=0;
CGFloat endA=M_PI;
UIBezierPath *path=[UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:NO];
//3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
//4.渲染上下文
CGContextStrokePath(ctx);
}
复制代码
- (void)drawRect:(CGRect)rect {
//1.获取上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();
//2.拼接路径
CGPoint center=CGPointMake(125, 125);
CGFloat radius=100;
CGFloat startA=0;
CGFloat endA=M_PI_2;
UIBezierPath *path=[UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
//3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
//4.渲染上下文
//CGContextStrokePath(ctx);
CGContextFillPath(ctx);
}
复制代码