贝塞尔曲线 UIBezierPath

#import "BezierPathView.h"


#define kDegreesToRadians(degrees) ((pi * degrees) / 180)


@implementation BezierPathView


- (void)drawRect:(CGRect)rect {

    

    // 画三角形

    [self drawTrianglePath];

    // 画矩形

    [self drawRectPath];

    // 画圆

    [self drawCiclePath];

    // 画带圆角的矩形

    [self drawRoundedRectPath];

    // 画弧

    [self drawARCPath];

    // 画二次贝塞尔曲线

    [self drawSecondBezierPath];

    // 画三次贝塞尔曲线

    [self drawThirdBezierPath];

    

}

- (void)drawTrianglePath

{

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:CGPointMake(20, 20)];

    [path addLineToPoint:CGPointMake(60, 20)];

    [path addLineToPoint:CGPointMake(40, 40)];

    [path closePath];

    // 设置线宽

    path.lineWidth = 1.5;

    // 设置填充颜色

    UIColor *fillColor = [UIColor greenColor];

    [fillColor set];

    [path fill];

    // 设置画笔颜色

    UIColor *strokeColor = [UIColor blueColor];

    [strokeColor set];

    

    // 根据我们设置的各个点连线

    [path stroke];

    

    

}


- (void)drawRectPath

{

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

    path.lineWidth = 1.5;

    /*

     kCGLineCapButt,    // 默认

     kCGLineCapRound,   // 轻微圆角

     kCGLineCapSquare   // 正方形

     */

    path.lineCapStyle = kCGLineCapRound;

    /*

     kCGLineJoinMiter,  // 默认表示斜接

     kCGLineJoinRound,  // 圆滑衔接

     kCGLineJoinBevel   // 斜角连接

     */

    path.lineJoinStyle = kCGLineJoinBevel;

    // 设置填充颜色

    UIColor *fillColor = [UIColor greenColor];

    [fillColor set];

    [path fill];

    // 设置画笔颜色

    UIColor *strokeColor = [UIColor blueColor];

    [strokeColor set];

    // 根据我们设置的各个点连接

    [path stroke];

}


- (void)drawCiclePath

{

    // 传的是正方形就可以画圆了(长方形--椭圆)

    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(230, 20, 100, 100)];

    // 设置填充颜色

    UIColor *fillColor = [UIColor greenColor];

    [fillColor set];

    [path fill];

    // 设置画笔颜色

    UIColor *strokeColor = [UIColor blueColor];

    [strokeColor set];

    // 根据我们设置的各个点连接

    [path stroke];

    

}


- (void)drawRoundedRectPath

{

    // 矩形的所有角都变成圆角

//    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 130, 100, 50) cornerRadius:10];

    // 指定矩形的某个角为圆角

    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 130, 100, 50) byRoundingCorners:UIRectCornerBottomRight cornerRadii:CGSizeMake(15, 15)];

    // 设置填充颜色

    UIColor *fillColor = [UIColor greenColor];

    [fillColor set];

    [path fill];

    // 设置画笔颜色

    UIColor *strokeColor = [UIColor blueColor];

    [strokeColor set];

    // 根据我们设置的各个点连线

    [path stroke];

}


// 画弧

- (void)drawARCPath

{

    const CGFloat pi = 3.14159265359;

    CGPoint center = CGPointMake(230, 230);

    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:100 startAngle:0 endAngle:kDegreesToRadians(145) clockwise:YES];

    // 有什么区别?

    path.lineCapStyle = kCGLineCapRound;

    path.lineJoinStyle = kCGLineJoinRound;

    path.lineWidth = 5.0;

    

    UIColor *strokeColor = [UIColor redColor];

    [strokeColor set];

    

    [path stroke];

}


// 画二次贝塞尔曲线

- (void)drawSecondBezierPath

{

    UIBezierPath *path = [UIBezierPath bezierPath];

    // 首先设置一个起点

    [path moveToPoint:CGPointMake(20, 230)];

    // 添加二次曲线(设置终端点和控制点)

    [path addQuadCurveToPoint:CGPointMake(120, 350) controlPoint:CGPointMake(self.frame.size.width / 2, 120)];

    path.lineCapStyle = kCGLineCapRound;

    path.lineJoinStyle = kCGLineJoinRound;

    path.lineWidth = 5.0;

    

    UIColor *strokeColor = [UIColor redColor];

    [strokeColor set];

    

    [path stroke];

}


// 画三次贝塞尔曲线

- (void)drawThirdBezierPath

{

    UIBezierPath *path = [UIBezierPath bezierPath];

    // 设置起始端点

    [path moveToPoint:CGPointMake(20, 150)];

    // 设置终止端点和控制点1、控制点2

    [path addCurveToPoint:CGPointMake(300, 150) controlPoint1:CGPointMake(160, 0) controlPoint2:CGPointMake(160, 250)];

    path.lineCapStyle = kCGLineCapRound;

    path.lineJoinStyle = kCGLineJoinRound;

    path.lineWidth = 5.0;

    

    UIColor *strokeColor = [UIColor redColor];

    [strokeColor set];

    

    [path stroke];

}

@end

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值