美文网首页
iOS UIBezierPath(贝塞尔曲线)

iOS UIBezierPath(贝塞尔曲线)

作者: 爪爪123 | 来源:发表于2020-06-16 15:51 被阅读0次

    原文地址: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

    相关文章

      网友评论

          本文标题:iOS UIBezierPath(贝塞尔曲线)

          本文链接:https://www.haomeiwen.com/subject/hpshxktx.html