iOS UIBezierPath(贝塞尔曲线)

2020-06-16  本文已影响0人  爪爪123

原文地址:https://www.jianshu.com/p/2256c9b4f031
1,空心椭圆

//bezierPathWithRoundedRect: 创建圆角矩形
 UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(110, 150, 150, 200) cornerRadius:100];
    CAShapeLayer * layer = [[CAShapeLayer alloc] init];
    layer.path = path.CGPath;
    layer.strokeColor = [UIColor redColor].CGColor;//线条颜色
    layer.fillColor = [UIColor clearColor].CGColor;// 空心
    layer.lineWidth = 5; //线条宽度
    [self.view.layer addSublayer:layer];

2,空心圆

  //bezierPathWithArcCenter:创建弧线路径
    UIBezierPath * path  = [UIBezierPath bezierPathWithArcCenter:self.view.center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
    
    CAShapeLayer * layer = [[CAShapeLayer alloc] init];
      layer.path = path.CGPath;
      layer.strokeColor = [UIColor redColor].CGColor;//线条颜色
      layer.fillColor = [UIColor clearColor].CGColor;// 空心
      layer.lineWidth = 5; //线条宽度
      [self.view.layer addSublayer:layer];

3,曲线
移动到起始点

path.moveToPoint(startPoint)

结束点和控制点

path.addQuadCurveToPoint(endPoint, controlPoint: controlPoint)

两个控制点可使用以下方法来搞定

path.addCurveToPoint(endPoint, controlPoint1: controlPoint, controlPoint2: controlPoint2)
  CGPoint  startPoint = CGPointMake(50, 300);
    CGPoint endPoint = CGPointMake(280, 300);
    CGPoint controlPoint = CGPointMake(170, 200);
    
//    红色方块
    CALayer * layer1 = [[CALayer alloc] init];
    layer1.frame = CGRectMake(startPoint.x, startPoint.y, 5, 5);
    layer1.backgroundColor = [UIColor redColor].CGColor;
    [self.view.layer addSublayer:layer1];
    
    CALayer *layer2 = [[CALayer alloc] init];
    layer2.frame = CGRectMake(endPoint.x, endPoint.y, 5, 5);
    layer2.backgroundColor = [UIColor purpleColor].CGColor;
    [self.view.layer addSublayer:layer2];
    
    CALayer * layer3 = [[CALayer alloc] init];
    layer3.frame = CGRectMake(controlPoint.x, controlPoint.y, 5, 5);
    layer3.backgroundColor = [UIColor orangeColor].CGColor;
    [self.view.layer addSublayer:layer3];
    
    
    UIBezierPath  * path = [[UIBezierPath alloc] init];
    [path moveToPoint:startPoint];
    
//    画两次贝塞尔曲线,一个控制点
    [path addQuadCurveToPoint:endPoint controlPoint:controlPoint];
    CAShapeLayer * layer = [[CAShapeLayer alloc] init];
    layer.path = path.CGPath;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.strokeColor = [UIColor grayColor].CGColor;
    [self.view.layer addSublayer:layer];
截屏2020-06-16下午1.28.35.png
上一篇下一篇

猜你喜欢

热点阅读