iOS开发中Quartz2D的基本使用方式举例

2020-01-14 17:25:19于海丽

//    CGContextSetRGBStrokeColor(ctx, 1.0, 0.7, 0.3, 1.0);
    //第二种设置颜色的方式
    [[UIColor grayColor] set];
    //设置线条的宽度
    CGContextSetLineWidth(ctx, 10);
    //设置线条的起点和终点的样式
    CGContextSetLineCap(ctx, kCGLineCapButt);
    
    //渲染第二条线的图形到view上
    //绘制一条空心的线
    CGContextStrokePath(ctx);
}


@end


效果:

 

iOS开发中Quartz2D的基本使用方式举例

二、画三角形

代码:

复制代码
//
//  YYrectview.m
//  02-画三角形
//
//  Created by 孔医己 on 14-6-10.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

 

#import "YYrectview.h"

@implementation YYrectview


- (void)drawRect:(CGRect)rect
{
    //1.获得图形上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    
    //2.绘制三角形
    //设置起点
    CGContextMoveToPoint(ctx, 20, 100);
    //设置第二个点
    CGContextAddLineToPoint(ctx, 40, 300);
    //设置第三个点
    CGContextAddLineToPoint(ctx, 200, 200);
    //设置终点
//     CGContextAddLineToPoint(ctx, 20, 100);
    //关闭起点和终点
    CGContextClosePath(ctx);
    
    // 3.渲染图形到layer上
    CGContextStrokePath(ctx);
    
}


@end


效果: