Quartz2D - 04.利用贝瑟尔路径(UIBezierPath)绘制基本图形

1.基本形状

// 自定义View
#import "DrawView.h"
@implementation DrawView
// 实现drawRect方法绘图
-(void)drawRect:(CGRect)rect{
    //  画线
//    [self drawLine1];
//    [self drawLine2];
    // 画曲线
//    [self drawQuadCurve];
    // 画形状
//    [self drawRect];
    // 画圆
//    [self drawCircle];
    // 画扇形
//    [self drawFan];
    // 画圆环
    [self drawLoop];
}

// 画圆环
-(void)drawLoop
{
    // 创建贝瑟尔路径
    UIBezierPath *path = [UIBezierPath bezierPath];

    // 设定圆心
    CGPoint center = CGPointMake(100, 100);

    // 画弧
    [path addArcWithCenter:center radius:50 startAngle:0 endAngle:M_PI * 2 clockwise:YES];

    // 画线
    [path addLineToPoint:center];

    // 关闭路径
    [path closePath];

    // 设置线宽
    path.lineWidth = 10;
    // 设置画笔颜色
    [[UIColor greenColor] setStroke];
    // 路径走线
    [path stroke];

    // 设置填充颜色
    [[UIColor redColor] setFill];
    // 填充路径
    [path fill];

}

// 画扇形
-(void)drawFan{

    // 创建贝瑟尔路径
    UIBezierPath *path = [UIBezierPath bezierPath];

    CGPoint center = CGPointMake(100, 100);
    // 画弧
    [path addArcWithCenter:center radius:50 startAngle:0 endAngle:M_PI_2 clockwise:YES];
    // 画线
    [path addLineToPoint:center];

    // 关闭路径
    [path closePath];

    // 设置画笔颜色
    [[UIColor redColor] set];
    // 画路径
    [path fill];


}

// 画圆形
-(void)drawCircle{
    // 创建贝瑟尔路径
    UIBezierPath *path = [UIBezierPath bezierPath];

    // 画线
    [path addArcWithCenter:CGPointMake(100, 100) radius:50 startAngle:0 endAngle:M_PI * 2 clockwise:YES];

    // 设置画笔颜色
    [[UIColor redColor] setStroke];
    // 画路径
    [path stroke];

}


// 画矩形
-(void)drawRect{

    // 创建贝瑟尔路径
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(20, 20, 100, 100)];

    // 设置属性
    // 设置线宽
    path.lineWidth = 20;
    // 设置交点样式
    path.lineJoinStyle = kCGLineJoinRound;
    // 设置画笔颜色
    [[UIColor redColor] setStroke];
    // 画路径
    [path stroke];


}


// 画曲线
-(void)drawQuadCurve{
    // 创建贝瑟尔路径
    UIBezierPath *path = [UIBezierPath bezierPath];

    // 画曲线
    // 设置起始点
    [path moveToPoint:CGPointMake(10, 100)];
    // 画曲线,根据控制点和终点
    [path addQuadCurveToPoint:CGPointMake(110, 100) controlPoint:CGPointMake(50, 10)];

    // 渲染路径
    [path stroke];
}




// 画线1
-(void)drawLine1{
    // 获取图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // 创建贝瑟尔路径
    UIBezierPath *path = [UIBezierPath bezierPath];


    // 移动到初始点
    [path moveToPoint:CGPointZero];

    // 画线
    [path addLineToPoint:CGPointMake(100, 100)];


    // 设置线宽
    CGContextSetLineWidth(ctx, 10);

    // 添加路径
    CGContextAddPath(ctx, path.CGPath);


    // 渲染视图
    CGContextStrokePath(ctx);
}
// 画线2
-(void)drawLine2{

    // 创建贝瑟尔路径
    UIBezierPath *path = [UIBezierPath bezierPath];
    // 移动到初始点
    [path moveToPoint:CGPointZero];

    // 画线
    [path addLineToPoint:CGPointMake(100, 100)];


    // 设置线宽
    path.lineWidth = 20;

    // 设置圆角
    path.lineCapStyle = kCGLineCapRound;

    // 渲染界面
    [path stroke];

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值