0
点赞
收藏
分享

微信扫一扫

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, 200, 50, 100, 400, 300, 400);
   CGContextStrokePath(context);
   
   
   //绘制连续的曲线
   CGContextSetLineWidth(context, 5.0);
   CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);
   CGContextMoveToPoint(context, 230, 150);//开始画线, x,y 为开始点的坐标
   CGContextAddCurveToPoint(context, 310, 100, 300, 200, 220, 220);//画三次点曲线
   CGContextAddCurveToPoint(context, 290, 140, 280, 180, 240, 190);//画三次点曲线
   
   CGContextStrokePath(context);//开始画线

举报

相关推荐

iOS 绘制贝塞尔曲线

曲线拟合-贝塞尔曲线

0 条评论