Quartz2D 绘制基本图形

利用Quartz2D可以绘制出系统不能提供的各种图形, 比如饼状图, 柱状图, 以及各种曲线, 所以熟练使用Quartz2D绘图还是有必要的, 下面是绘制基本图形

//  Quartz2D基本图形绘制
//
//  Created by Wangjunling on 16/3/9.
//  Copyright © 2016年 Wangjunling. All rights reserved.
//

#import "WJLView.h"


@implementation WJLView

- (void)drawRect:(CGRect)rect {
//    [self test1];//画线
//    [self test2];//画矩形
    [self test6];
}

- (void)test1 {  //画线
    //获取当前绘图上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //设置起点
    CGContextMoveToPoint(ctx, 100, 100);
    //设置下一个点
    CGContextAddLineToPoint(ctx, 100, 200);
    CGContextAddLineToPoint(ctx, 200, 200);
    //设置线宽
    CGContextSetLineWidth(ctx, 10);
    //设置线头的样式(圆头)
    CGContextSetLineCap(ctx, kCGLineCapRound);
    //设置两条线连接处的样式
    CGContextSetLineJoin(ctx, kCGLineJoinMiter);
    //绘制图形
    CGContextStrokePath(ctx);
    
}

- (void)test2 {  //画矩形
    //获取当前绘图上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextAddRect(ctx, CGRectMake(0, 0, 200, 200));
    
    //绘制图形
    CGContextStrokePath(ctx);
    //填充模式绘制
    //
    CGContextFillPath(ctx);
    
}

- (void)test3 {  //画圆形
    //获取当前绘图上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //方式一
    CGContextAddEllipseInRect(ctx, CGRectMake(0, 50, 300, 200));
    //方式二;
    CGContextAddArc(ctx, 150, 150, 50, 0, M_PI*2, 1);
    
    //绘制图形
    CGContextStrokePath(ctx);
    
}

- (void)test4 {//扇形
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //设置起点为圆心
    CGContextMoveToPoint(ctx, 150, 150);
    CGContextAddArc(ctx, 150, 150, 100, 0, M_PI_4, 0);
    //封闭路径
    CGContextClosePath(ctx);

    CGContextStrokePath(ctx);
}

- (void)test5 {//画同心圆
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextAddArc(ctx, 150, 150, 50, 0, M_PI*2, 0);
    //移除两个圆的链接线
    CGContextMoveToPoint(ctx, 250, 150);
    CGContextAddArc(ctx, 150, 150, 100, 0, M_PI*2, 0);
}

- (void)test6 {//虚线
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextMoveToPoint(ctx, 0, 100);
    CGContextAddLineToPoint(ctx, 300, 100);
    //设置空实比例, 第一个参数为第一段的
    CGFloat lengths[4] = {10, 5, 10, 5};
    
    CGContextSetLineDash(ctx, 0, lengths, 4 );
    CGContextStrokePath(ctx);
}

- (void)test7 {//贝塞尔曲线
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //起始点
    CGContextMoveToPoint(ctx, 50, 200);
    //控制点和结束点
    CGContextAddQuadCurveToPoint(ctx, 150, 50, 250, 200);
    
    CGContextStrokePath(ctx);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值