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

2020-01-14 17:25:19于海丽
iOS开发中Quartz2D的基本使用方式举例

提示:关闭起点和终点  CGContextClosePath(ctx);

三、画四边形

代码:

复制代码
//
//  YYrect.m
//  03-画四边形
//
//  Created by 孔医己 on 14-6-10.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

 

#import "YYrect.h"

@implementation YYrect


- (void)drawRect:(CGRect)rect
{

    //1.获取图形上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    //2.画四边形
    CGContextAddRect(ctx, CGRectMake(20, 20, 150, 100));
    
    // 如果要设置绘图的状态必须在渲染之前
    //    CGContextSetRGBStrokeColor(ctx, 1.0, 0, 0, 1.0);
    // 绘制什么类型的图形(空心或者实心).就要通过什么类型的方法设置状态
    //    CGContextSetRGBFillColor(ctx, 1.0, 0, 0, 1.0);
    
    // 调用OC的方法设置绘图的颜色
    //    [[UIColor purpleColor] setFill];
    //    [[UIColor blueColor] setStroke];
    // 调用OC的方法设置绘图颜色(同时设置了实心和空心)
    //    [[UIColor greenColor] set];
    [[UIColor colorWithRed:1.0 green:0 blue:0 alpha:1.0] set];
    
    
    //3.渲染图形到layer上
    //空心的
    CGContextStrokePath(ctx);
    //实心的
//    CGContextFillPath(ctx);
    
}


@end


提示:如果要设置绘图的状态必须在渲染之前。

 

效果(实心和空心):