iOS-UIBezierPath绘制基本图形

UIBezierPath
UIBezierPath可以创建基于矢量的路径,此类是Core Graphics框架关于路径的封装。使用此类可以定义简单的形状,如椭圆、矩形或者有多个直线和曲线段组成的形状等

使用UIBezierPath画图步骤:
1、创建一个UIBezierPath对象
2、调用-moveToPoint:设置初始线段的起点
3、添加线或者曲线去定义一个或者多个子路径
4、改变UIBezierPath对象跟绘图相关的属性。如,我们可以设置画笔的属性、填充样式等

/*
 * 绘制三角形
 */
 - (void)drawRect:(CGRect)rect {
    // Drawing code
    //初始化路径
    UIBezierPath *path = [UIBezierPath bezierPath];
    //设置路径第一个点
    [path moveToPoint:CGPointMake(50, 50)];
    //设置路径第二个点
    [path addLineToPoint:CGPointMake(self.bounds.size.width-50, 50)];
    //设置路径第三个点
    [path addLineToPoint:CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height - 50)];
    //闭合路径
    [path closePath];
    //设置线宽
    path.lineWidth = 10;
    //设置填充颜色
    UIColor *fillColor = [UIColor redColor];
    [fillColor set];
    [path fill];
    //设置画笔颜色
    UIColor *strokeColor = [UIColor greenColor];
    [strokeColor set];
    [path stroke];
}

这里写图片描述


/*
 * 绘制矩形
 */
- (void)drawRect:(CGRect)rect {
    // Drawing code
    //初始化矩形路径
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(50, 50, self.bounds.size.width-100, self.bounds.size.width-100)];
    //设置线宽
    path.lineWidth = 10;
    //设置填充颜色
    UIColor *fillColor = [UIColor redColor];
    [fillColor set];
    [path fill];
    //设置画笔颜色
    UIColor *strokeColor = [UIColor greenColor];
    [strokeColor set];
    [path stroke];
}

这里写图片描述


/*
 * 绘制圆角矩形
 */
- (void)drawRect:(CGRect)rect {
    // Drawing code
    //初始化路径
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 50, self.bounds.size.width-100, self.bounds.size.width-100) cornerRadius:50];
    //设置线宽
    path.lineWidth = 10;
    //设置填充颜色
    [[UIColor redColor] set];
    [path fill];
    //设置画笔颜色
    [[UIColor greenColor] set];
    [path stroke];
}

这里写图片描述

/*
 * 绘制圆角矩形2
 */
- (void)drawRect:(CGRect)rect {
    // Drawing code
    //初始化路径
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 50, self.bounds.size.width-100, self.bounds.size.width-100) byRoundingCorners:UIRectCornerTopRight cornerRadii:CGSizeMake(200, 200)];
    //设置线宽
    path.lineWidth = 10;
    //设置填充颜色
    [[UIColor redColor] set];
    [path fill];
    //设置画笔颜色
    [[UIColor greenColor] set];
    [path stroke];
}

这里写图片描述


/*
 * 绘制梯形
 */
- (void)drawRect:(CGRect)rect {
    // Drawing code
    //初始化路径
    UIBezierPath *path = [UIBezierPath bezierPath];
    //设置路径第一个点
    [path moveToPoint:CGPointMake(50, 50)];
    //设置路径第二个点
    [path addLineToPoint:CGPointMake(50, 150)];
    //设置路径第三个点
    [path addLineToPoint:CGPointMake(300, 150)];
    //设置路径第四个点
    [path addLineToPoint:CGPointMake(250, 50)];
    //闭合路径
    [path closePath];
    //设置线宽
    path.lineWidth = 10;
    //设置填充颜色
    UIColor *fillColor = [UIColor redColor];
    [fillColor set];
    [path fill];
    //设置画笔颜色
    UIColor *strokeColor = [UIColor greenColor];
    [strokeColor set];
    [path stroke];
}

这里写图片描述



/*
 * 绘制圆
 */
- (void)drawRect:(CGRect)rect {
    // Drawing code
    //初始化矩形路径
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, self.bounds.size.width-100, self.bounds.size.width-100)];
    //设置线宽
    path.lineWidth = 10;
    //设置填充颜色
    UIColor *fillColor = [UIColor redColor];
    [fillColor set];
    [path fill];
    //设置画笔颜色
    UIColor *strokeColor = [UIColor greenColor];
    [strokeColor set];
    [path stroke];
}

这里写图片描述



/*
 * 绘制椭圆
 */
- (void)drawRect:(CGRect)rect {
    // Drawing code
    //初始化路径
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, self.bounds.size.width-100, self.bounds.size.width-200)];
    //设置线宽
    path.lineWidth = 10;
    //设置填充颜色
    UIColor *fillColor = [UIColor redColor];
    [fillColor set];
    [path fill];
    //设置画笔颜色
    UIColor *strokeColor = [UIColor greenColor];
    [strokeColor set];
    [path stroke];
}

这里写图片描述



/*
 * 绘制圆弧
 */
- (void)drawRect:(CGRect)rect {
    // Drawing code
    //设置中心点
    CGPoint center = CGPointMake(self.bounds.size.width * 0.4, self.bounds.size.width * 0.4);
    //绘制路径,最后一个参数是圆弧的方向
    UIBezierPath *arcPath = [UIBezierPath bezierPathWithArcCenter:center radius:200 startAngle:0 endAngle:M_2_PI*3 clockwise:YES];
    //设置线宽
    arcPath.lineWidth = 5;
    //设置线断面类型
    arcPath.lineCapStyle = kCGLineCapRound;

    arcPath.lineJoinStyle = kCGLineJoinRound;
    设置画笔颜色
    [[UIColor redColor] set];
    [arcPath stroke]; 
}

这里写图片描述



/*
 * 绘制二次贝塞尔曲线
 */
- (void)drawRect:(CGRect)rect {
    // Drawing code
    //初始化UIBezierPath
    UIBezierPath *path = [UIBezierPath bezierPath];
    //首先设置一个起始点
    [path moveToPoint:CGPointMake(50, 200)];
    //首先设置一个终点
    CGPoint endPoint = CGPointMake(300, 300);
    //首先设置一个控制点
    CGPoint controlPoint = CGPointMake(180, 30);
    //添加二次曲线
    [path addQuadCurveToPoint:endPoint controlPoint:controlPoint];

    //设置线宽
    path.lineWidth = 5;
    //设置线断面类型
    path.lineCapStyle = kCGLineCapRound;

    path.lineJoinStyle = kCGLineJoinRound;
    设置画笔颜色
    [[UIColor redColor] set];
    [path stroke];
}

这里写图片描述



/*
 * 绘制三次贝塞尔曲线
 */
- (void)drawRect:(CGRect)rect {
    // Drawing code
    //初始化UIBezierPath
    UIBezierPath *path = [UIBezierPath bezierPath];
    //首先设置一个起始点
    [path moveToPoint:CGPointMake(30, 150)];
    //首先设置一个终点
    CGPoint endPoint = CGPointMake(300, 150);
    //首先设置第一个控制点
    CGPoint controlPoint1 = CGPointMake(160, 30);
    //首先设置第二个控制点
    CGPoint controlPoint2 = CGPointMake(160, 250);

    //添加三次贝塞尔曲线
    [path addCurveToPoint:endPoint controlPoint1:controlPoint1 controlPoint2:controlPoint2];

    //设置线宽
    path.lineWidth = 5;
    //设置线断面类型
    path.lineCapStyle = kCGLineCapRound;

    path.lineJoinStyle = kCGLineJoinRound;
    设置画笔颜色
    [[UIColor redColor] set];
    [path stroke];
}

这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值