iOS 画贝塞尔曲线代码

#import "ViewController.h"


#define pi 3.14159265359

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


@interface ViewController ()


@property(nonatomic, strong) CAShapeLayer *shapeLayer;


@end


    self.shapeLayer = [CAShapeLayer layer];

    _shapeLayer.frame = CGRectMake(0, 0, 200, 200);

    _shapeLayer.position = self.view.center;

    _shapeLayer.fillColor = [UIColor clearColor].CGColor;

    

    _shapeLayer.lineWidth = 1.0f;

    _shapeLayer.strokeColor = [UIColor redColor].CGColor;

    

//    UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 200, 200)];

    

//    UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 200, 200)];

    

    

//    UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150)

//                                                         radius:75

//                                                     startAngle:0

//                                                       endAngle:DEGREES_TO_RADIANS(135)

//                                                      clockwise:YES];

    

//    //绘制二次贝塞尔曲线

//    UIBezierPath *circlePath = [UIBezierPath bezierPath];

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

//    [circlePath moveToPoint:CGPointMake(0, 100)];

//    [circlePath addQuadCurveToPoint:CGPointMake(200, 100) controlPoint:CGPointMake(100, 0)];

    

    

    //绘制三次贝塞尔曲线

    //绘制二次贝塞尔曲线

    UIBezierPath *circlePath = [UIBezierPath bezierPath];

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

//    [circlePath moveToPoint:CGPointMake(0, 100)];

//    [circlePath addQuadCurveToPoint:CGPointMake(200, 100) controlPoint:CGPointMake(100, 0)];

    

    [circlePath moveToPoint:CGPointMake(20, 50)];

    

    [circlePath addCurveToPoint:CGPointMake(200, 50) controlPoint1:CGPointMake(110, 0) controlPoint2:CGPointMake(110, 100)];

    

    

    _shapeLayer.path = circlePath.CGPath;

    

    

    _shapeLayer.strokeStart = 0;

    _shapeLayer.strokeEnd = 1.0;

    

    [self.view.layer addSublayer:_shapeLayer];


    //绘制贝兹曲线

    //贝兹曲线是通过移动一个起始点,然后通过两个控制点,还有一个中止点,调用CGContextAddCurveToPoint() 函数绘制

    CGContextSetLineWidth(context, 2.0);

    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);

    CGContextMoveToPoint(context, 10, 10);

    CGContextAddCurveToPoint(context, 20050100400300400);

    CGContextStrokePath(context);

    

    

    //绘制连续的曲线

    CGContextSetLineWidth(context, 5.0);

    CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);

    CGContextMoveToPoint(context, 230, 150);//开始画线, xy 为开始点的坐标

    CGContextAddCurveToPoint(context, 310100300200220220);//画三次点曲线

    CGContextAddCurveToPoint(context, 290140280180240190);//画三次点曲线

    

    CGContextStrokePath(context);//开始画线


你可能感兴趣的:(iOS 画贝塞尔曲线代码)