IOS学习 绘图 UIBezierPath 绘基本图形、样式设置、渲染填充

本文详细介绍了在iOS开发中如何使用UIBezierPath进行图形绘制,包括矩形、圆、椭圆、弧线等基本图形的绘制,并讲解了图形样式设置和渲染填充的方法。通过实例代码展示了如何在drawRect:方法中操作图形上下文来实现自定义视图的绘图效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import "CZView.h"


@implementation CZView


-(void)drawRect:(CGRect)rect{

    //1.为什么要重写drawRect:方法

    // 只有在drawRect:才能获取上下文对象  --->才能绘图

    //2.rect参数:就是指当前视图的bounds属性

    

    //3.什么时候调用drawRect:方法

    

    // 1.视图第一次被显示 的时候 系统调用

    

    // 2.执行重绘的时候

    

    //4.不要手动调用这个方法 (系统会自动调用) drawRect:

    

    // 5.如果要手动调用执行重绘 [self setNeedsDisplay] / [self setNeedsDisplayInRect:<#(CGRect)#>]

    

    [self demo8];

}


- (void) demo9{

    // 1. 获取"图形上下文"

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // 2.创建矩形路径

    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(100, 100, 200, 100)];

    // 3.创建一个圆心路径

    UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(200, 150) radius:80 startAngle:0 endAngle:M_PI * 2 clockwise:1];

    // 4.创建一个矩形路径

    UIBezierPath *path2 = [UIBezierPath bezierPathWithRect:CGRectMake(250, 30, 20, 200)];

    

    // 5.把刚刚创建的路径依次添加到上下文对象中

    CGContextAddPath(ctx, path2.CGPath);

    CGContextAddPath(ctx, path1.CGPath);

    CGContextAddPath(ctx, path.CGPath);

    

    // 说明: 被覆盖过奇数次的点填充, 被覆盖过偶数次的点不填充

    // 6.渲染

    CGContextDrawPath(ctx, kCGPathEOFill);

}


-(void)demo8{

    // 1. 获取"图形上下文"

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    //2.以顺时针的方式画一个大圆

    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:0 endAngle:M_PI * 2 clockwise:YES];

    // 3.以逆时针画一个小圆

    UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:50 startAngle:0 endAngle:M_PI * 2 clockwise:NO];

    //4.添加路径到上下文中

    CGContextAddPath(ctx, path1.CGPath);

    CGContextAddPath(ctx, path.CGPath);

    

    // 默认填充模式: nonzero winding number rule(非零绕数规则)从左到右跨过, +1。从右到左跨过, -1。最后如果为0, 那么不填充, 否则填充

    //渲染

    CGContextDrawPath(ctx, kCGPathFill);

}


//绘制弧线

-(void)demo7{

    //获取上下文对象

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    

    //创建路径对象

    //1.圆心

    CGPoint centerP = CGPointMake(150, 150);

    

    //2.半径

    CGFloat radius = 100;

    

    //3.起始角度

    CGFloat start = 0;

    

    //4.结束角度

    CGFloat end = 0.5*M_PI; //2Pi为整圆

    

    //5.clockwise 顺时针:YES   逆时针:NO

    

    UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:centerP radius:radius startAngle:start endAngle:end clockwise:YES];

    

    //回到圆心,画扇形

    [path addLineToPoint:centerP];

    [path closePath];  //关闭路径


    //将路径对象加入上下文对象中

    CGContextAddPath(ctx, path.CGPath);

    

    //渲染

    CGContextStrokePath(ctx);

}



//绘制椭圆形

-(void)demo6{

    //获取上下文对象

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    

    //创建路径对象

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

    

    //将路径对象加入上下文对象中

    CGContextAddPath(ctx, path.CGPath);

    

    //渲染

//    CGContextStrokePath(ctx);

    CGContextFillPath(ctx);

}



//绘制圆形

-(void)demo5{

    //获取上下文对象

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    

    //创建路径对象

    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 50, 200, 200) cornerRadius:100];

    

    //将路径对象加入上下文对象中

    CGContextAddPath(ctx, path.CGPath);

    

    //渲染

    CGContextStrokePath(ctx);

}


//绘制带圆角的矩形

-(void)demo4{

    //获取上下文对象

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    

    //创建路径对象

    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 50, 200, 100) cornerRadius:30];

    

    //将路径对象加入上下文对象中

    CGContextAddPath(ctx, path.CGPath);

    

    //渲染

    CGContextFillPath(ctx);

}


//绘制正方形(简单写法)

-(void)demo3{

    //获取上下文对象

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    

    //创建路径添加到上下文中

    CGContextAddRect(ctx, CGRectMake(50, 50, 100, 100));

    

    //设置绘图样式

    //1 设置线的stroke颜色

        [[UIColor redColor] setStroke];

    //设置填充颜色

    //    [[UIColor greenColor] setFill];

    //设置颜色

//    [[UIColor blueColor] set];

    //2.1.2 设置线宽

    CGContextSetLineWidth(ctx, 10);

    

    //2.1.3 设置线头样式

    CGContextSetLineCap(ctx, kCGLineCapRound);

    

    //2.1.4 设置线的连接处样式

//    CGContextSetLineJoin(ctx, kCGLineJoinRound);

    

    //渲染

    CGContextStrokePath(ctx);

//    CGContextFillPath(ctx);

}


//绘制四边形

-(void)demo2{

    //获取上下文对象

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    

    //创建路径对象

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

    

    //将路径对象加入上下文对象中

    CGContextAddPath(ctx, path.CGPath);//path转化成CGPath

    

    //炫染

    //1.边线样式

//    CGContextDrawPath(ctx, kCGPathStroke);

//    CGContextStrokePath(ctx);

    

    //2.填充样式

//    CGContextDrawPath(ctx, kCGPathFill);

    CGContextFillPath(ctx);

}


//绘制线段

-(void)demo1{

    //获取上下文对象

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    

    //创建路径,添加到上下文对象中

    //1.确定起点

    CGContextMoveToPoint(ctx, 50, 50);

    

    //2.添加一条线

    CGContextAddLineToPoint(ctx, 200, 200);

    

    //3.再添加一条线

    CGContextAddLineToPoint(ctx, 50, 200);

    

    //4.闭合的线

    CGContextAddLineToPoint(ctx, 50, 50);

//    CGContextClosePath(ctx);

    

    //另画一条线

    CGContextMoveToPoint(ctx, 100, 50);

    CGContextAddLineToPoint(ctx, 250, 50);


    //炫染

    CGContextStrokePath(ctx);

}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值