iOS Quartz绘图

//
//  DrawView.m
//  Demo
//
//  Created by llkj on 2017/8/21.
//  Copyright © 2017年 LayneCheung. All rights reserved.
//

#import "DrawView.h"

@implementation DrawView


- (void)drawRect:(CGRect)rect {

    //画弧
    //Center:弧所在的圆心
    //radius:圆的半径
    //startAngle:开始角度
    //endAngle:截至角度
    //clockwise: YES:顺时针 NO:逆时针

    NSLog(@"%@",NSStringFromCGPoint(self.center));

    CGPoint center = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5);
    CGFloat radius = rect.size.width * 0.5 - 10;
    //不能直接会用self.center ,是因为self.center座标是相对于它的父控件.
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:-M_PI_2 clockwise:NO];

    //添加一根线到圆心
    [path addLineToPoint:center];

    //关闭路径closePath:从路径终点连接一根线到路径的起点
    //[path closePath];

    [[UIColor redColor] set];

    //画扇形
    //fill(填充之前,会自动关闭路径)
    [path fill];
}

//画椭圆
- (void)drawOval{

    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 100, 50)];

    [[UIColor orangeColor] set];
    [path stroke];//1.获取上下文->2.绘制路径->3.把绘制的内容添加到上下文中->4.把上下文添加到View上
    //[path fill];

}

//画直线
- (void)drawLine{

    //1.获取上下文
    CGContextRef ref = UIGraphicsGetCurrentContext();

    //2.绘制路径
    UIBezierPath *path = [UIBezierPath bezierPath];

    //2.1设置起点
    [path moveToPoint:CGPointMake(50, 250)];

    //2.2添加一根线到终点
    [path addLineToPoint:CGPointMake(250, 50)];

    [path addLineToPoint:CGPointMake(100, 280)];

    //上下文的状态

    //设置线的宽度
    CGContextSetLineWidth(ref, 10);

    //设置线的连接样式
    CGContextSetLineJoin(ref, kCGLineJoinRound);

    //设置线的顶角样式
    CGContextSetLineCap(ref, kCGLineCapRound);

    //设置颜色
    [[UIColor redColor] setStroke];

    //3.把绘制的内容添加到上下文中
    CGContextAddPath(ref, path.CGPath);

    //4.把上下文添加到View上
    CGContextStrokePath(ref);
}

//画矩形
- (void)drawRect{

    CGContextRef ref = UIGraphicsGetCurrentContext();

    //    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(50, 50, 200, 100)];矩形

    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 50, 100, 100) cornerRadius:100];//圆角矩形

    [[UIColor orangeColor] set];

    CGContextAddPath(ref, path.CGPath);

    //    CGContextStrokePath(ref);//描边
    CGContextFillPath(ref);//填充

}

//画曲线
- (void)drawQuadCurve{

    CGContextRef ref = UIGraphicsGetCurrentContext();

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:CGPointMake(50, 280)];

    [path addQuadCurveToPoint:CGPointMake(250, 280) controlPoint:CGPointMake(50, 50)];

    CGContextAddPath(ref, path.CGPath);

    CGContextStrokePath(ref);

}

@end
发布了55 篇原创文章 · 获赞 1 · 访问量 3万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章