IOS BezierPath 贝塞尔曲线画图详解

来源:互联网 发布:沪深300指数基金 知乎 编辑:程序博客网 时间:2024/05/19 00:14

   

 //UIColor *blueColor=[UIColor redColor];

    // 画一个矩形

//    UIBezierPath *pathRect=[UIBezierPath bezierPathWithRect:CGRectMake(20, 30, 200, 300)];

//    UIColor *redColor=[UIColor redColor];

//    [redColor set];

//    [pathRect stroke];

    

    

    // 在矩形里面画一个椭圆

//    UIBezierPath *pathOval=[UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 30, 200, 300)];

//    

//   

//    [blueColor set];

//    [pathOval stroke];

    

//  画一个圆角矩形

    //UIBezierPath *pathRoundCorner=[UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 30, 200, 300) cornerRadius:5];

//   

//    [blueColor set];

//    [pathRoundCorner stroke];

  // 一个矩形选择其实的几个角是圆角的

//    UIBezierPath *pathRoundCorners=[UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 30, 200, 300) byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(10, 20)];

//    [blueColor set];

//    [pathRoundCorners stroke];


 //画圆弧  0°是水平开始的

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

//    

//    [blueColor set];

//    [pathArc stroke];

    

    

   // + (instancetype)bezierPathWithCGPath:(CGPathRef)CGPath; 这个功能没弄明白

  //在不同的点之间连线

//    UIBezierPath *path=[[UIBezierPath alloc]init];

//    path.lineWidth=5;

//    path.lineCapStyle=kCGLineCapButt;

//    path.lineJoinStyle=kCGLineJoinBevel;

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

//    [path addLineToPoint:CGPointMake(80, 80)];

//    [path addLineToPoint:CGPointMake(20, 160)];

//    

//    [path closePath];

//    

//    [blueColor set];

//    [path stroke];

    

   //  三次贝塞尔曲线

//    UIBezierPath *path=[[UIBezierPath alloc]init];

//    

//    path.lineWidth=5;

//    path.lineJoinStyle=kCGLineJoinRound;

//    path.lineCapStyle=kCGLineCapButt;

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

//    [path addCurveToPoint:CGPointMake(240, 80) controlPoint1:CGPointMake(80, 40) controlPoint2:CGPointMake(160, 160)];

//    [blueColor set];

//    [path stroke];

//    

//    UIBezierPath *path=[[UIBezierPath alloc]init];

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

//    

//    [path addQuadCurveToPoint:CGPointMake(120, 120) controlPoint:CGPointMake(60, 40)];

//    

//   [blueColor set];

//    

//    [path stroke];

  

//    UIBezierPath *path=[[UIBezierPath alloc]init];

//    //path.flatness=80;

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

//    path.usesEvenOddFillRule=YES;

//    [blueColor set];

//    

//        [path stroke];

    


//画虚线


 UIBezierPath *path=[[UIBezierPathalloc]init];

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

    [path addLineToPoint:CGPointMake(180,100)];

   CGFloat lengths[] = {20,10,5};

    //这是一个C 数组 意思就是先画20个像素长度的线段然后空10个像素    长度然后再画5个像素长度的线段反复循环下去。

    [pathsetLineDash:lengthscount:3 phase:0];

    //这里的phase是指位移  如果phase的参数是6 那么首先就是画(20-6)个像素长度的线段。  

    [[UIColorredColor]set];

    [pathstroke];


以上贝塞尔曲线需要在view的函数-(void)drawRect:(CGRect)rect里面执行 但是也可以在view controller定义CAShapeLayer把画的曲线展现出来。


CAShapeLayer *layer = [[CAShapeLayer alloc]init];

 layer.frame=CGRectMake(0, 0, 200, 400);

 [self.view.layer addSublayer:layer];

 

 

 UIBezierPath *bezierPath = [UIBezierPath bezierPath];

 UIBezierPath *outerCircle = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(CGRectMake(0, 0, 150, 200), 4, 4)];

 [outerCircle setLineWidth:2.0];

 [bezierPath appendPath:outerCircle];

 

 UIBezierPath *innerCircle = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(CGRectMake(50, 20, 150, 200), 20, 20)];

 [innerCircle setLineWidth:2.0];

 [bezierPath appendPath:innerCircle];

 bezierPath.usesEvenOddFillRule = YES;

 

 layer.path = bezierPath.CGPath;

 layer.fillColor = [UIColor orangeColor].CGColor;

 layer.strokeColor = [UIColor redColor].CGColor;


另外函数

- (void)fillWithBlendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;

- (void)strokeWithBlendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;

可以改变所画出图片的透明度但是不改变当前画布的透明度





1 0
原创粉丝点击