33.画线,三角形,四边形

1.画圆:

// 当自定义view第一次显示出来的时候就会调用drawRect方法
- (void)drawRect:(CGRect)rect
{
    // 1.取得和当前视图相关联的图形上下文
    //因为图形上下文决定绘制的输出目标

    // 如果是在drawRect方法中调用UIGraphicsGetCurrentContext方法获取出来的就是Layer的上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // 2.绘图(绘制直线), 保存绘图信息
    // 设置起点
    CGContextMoveToPoint(ctx, 10, 100);
    // 设置终点
    CGContextAddLineToPoint(ctx, 100, 100);

    // 设置绘图状态
    // 设置线条颜色 红色
    CGContextSetRGBStrokeColor(ctx, 1.0, 0, 0, 1.0);
    // 设置线条宽度
    CGContextSetLineWidth(ctx, 10);
    // 设置线条的起点和终点的样式
    CGContextSetLineCap(ctx, kCGLineCapRound);
    // 设置线条的转角的样式
    CGContextSetLineJoin(ctx, kCGLineJoinRound);
    // 绘制一条空心的线
    CGContextStrokePath(ctx);

    /*---------------------------------------*/

    // 重新设置第二条线的起点
    CGContextMoveToPoint(ctx, 150, 200);
    // 设置第二条直线的终点(自动把上一条直线的终点当做起点)
    CGContextAddLineToPoint(ctx, 100, 50);
    // 设置第二条线的颜色 绿色
//    [[UIColor greenColor] set];
    CGContextSetRGBStrokeColor(ctx, 0, 1.0, 0, 1.0);

    // 绘制图形(渲染图形到view上)
    // 绘制一条空心的线
    CGContextStrokePath(ctx);

}
2.画三角形:

-(void)drawSanJiao
{
    // 1.获取图形上下文
    CGContextRef ctx =  UIGraphicsGetCurrentContext();

    // 2. 绘制三角形
    // 设置起点
    CGContextMoveToPoint(ctx, 100, 10);
    // 设置第二个点
    CGContextAddLineToPoint(ctx, 50, 100);
    // 设置第三个点
    CGContextAddLineToPoint(ctx, 150, 100);
    // 设置终点
    //    CGContextAddLineToPoint(ctx, 100, 10);
    ***// 关闭起点和终点
    CGContextClosePath(ctx);***
    // 3.渲染图形到layer上
    CGContextStrokePath(ctx);
}
3.画四边形:

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    // 绘制四边形
    // 1.获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 2.绘制四边形
    CGContextAddRect(ctx, CGRectMake(10, 10, 150, 100));

    // 如果要设置绘图的状态必须在渲染之前

//    绘制什么类型的图形(空心或者实心).就要通过什么类型的方法设置状态
//    CGContextSetRGBFillColor(ctx, 1.0, 0, 0, 1.0);
//    CGContextFillPath(ctx);
//    CGContextSetRGBStrokeColor(ctx, 1.0, 0, 0, 1.0);
//    CGContextStrokePath(ctx);

//     调用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);

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值