基础绘制

1.基础画线
OC:

1.获取上下文
CGContextRef cxt = UIGraphicsGetCurrentContext()
2.得到贝塞尔曲线
UIBezierPath *path = [UIBezierPath bezierPath]
3.设置起点
[path moveToPoint:CGPointMake(0, 0)];
4.增加一根线到终点
[path addLineToPoint:CGPointMake(100, 100)];
5.再增加一条线(起点是上条线的终点)
[path addLineToPoint:CGPointMake(200, 150)];
6.设置线宽
CGContextSetLineWidth(ctx, 5);
7.设置顶角样式
CGContextSetLineCap(ctx, kCGLineCapRound);
8.设置线连接样式
CGContextSetLineJoin(ctx, kCGLineJoinRound);
9.设置颜色
[[UIColor redColor] set];
10.把绘制的内容添加到上下文中
CGContextAddPath(ctx, path.CGPath);
11.把上下文渲染出来
CGContextStrokePath(ctx);

swift:

let ref = UIGraphicsGetCurrentContext()

let path = UIBezierPath()
        
path.moveToPoint(CGPointMake(0, 0))
        
path.addLineToPoint(CGPointMake(100, 100))
        
path.addLineToPoint(CGPointMake(200, 200))
        
CGContextSetLineWidth(ref, 5)
        
CGContextSetLineCap(ref, CGLineCap.Round)
        
CGContextSetLineJoin(ref, .Round)
        
UIColor.redColor().set()
        
CGContextAddPath(ref, path.CGPath)
        
CGContextStrokePath(ref)

2.画弧线
OC

[path addQuadCurveToPoint:CGPointMake(200, 200) controlPoint:CGPointMake(0, 200)];

Swift

path.addQuadCurveToPoint(CGPointMake(200, 200), controlPoint: CGPointMake(0, 200))

3.画圆(椭圆)
OC

UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 100, 100)];
[path setLineWidth:1]
[[UIColor redColor] set];
[path stroke];

Swift

let path = UIBezierPath(ovalInRect: CGRectMake(1, 1, 200, 200))
UIColor.redColor().set()
path.lineWidth = 1
path.stroke()

4.画弧
OC

//Cententer:圆弧所在的圆心
//radius:半径
//startAngle:开始角度
//endAngle:截止角度
//clockwise:YES:顺时针 NO:逆时针
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 100) radius:30 startAngle:M_PI endAngle:0 clockwise:NO];
[path setLineWidth:1]
    //添加一根线到圆心
[path addLineToPoint:CGPointMake(100, 100)];
[[UIColor redColor] set];
    //关闭路劲:从路劲终点连接一个线到路劲的起点
[path closePath]
[path stroke];
    //填充(填充会自动关闭路径)
    //[path fill];

Swift:

let path = UIBezierPath(arcCenter: CGPointMake(100, 100), radius: 30, startAngle: CGFloat(M_PI), endAngle: 0, clockwise: false)
path.addLineToPoint(CGPointMake(100, 100))
UIColor.redColor().set()
path.lineWidth = 1
path.closePath()
path.stroke()

后续更新中....

你可能感兴趣的:(基础绘制)