UIBezierPath路径具有多个线宽

时间:2014-10-17 23:15:06

标签: ios core-graphics uibezierpath

我正在尝试构建一个设计应用程序,其中一个功能允许人们在各种资产上预览不同类型的自定义边框。

我正在尝试使用UIBezierPath绘制这些边框。

现在系统将支持:

  1. 各种边界半径(0 - >任何其他地方计算的天花板)
  2. 整个边框的不同颜色
  3. 我正在尝试实施:

    1. 具有不同边框线宽的能力。
    2. 我认为我应该能够通过绘制不同宽度的每个路径来实现这一点,但我无法弄清楚如何将其从路径更改为路径。谁能帮我吗?

      感谢。

      Click top see the border script in action

      +(void)setBorderOnView :(id)object withWidth:(float)width andBorders:(NSArray*)borders ofColor:(UIColor*)color andRadius:(float)radius andRadii:(NSArray*)radii
      {
      
      UIView *objectView = object;
      
      // Add half a pixel to compensate for border stroke, which puts .5 pixels outside of view.
      float borderWidth = width+0.5;
      
      float topLeftRadius = radius;
      float topRightRadius = radius;
      float bottomRightRadius = radius;
      float bottomLeftRadius = radius;
      
      CAShapeLayer *roundedCornerLayer = [CAShapeLayer layer];
      CAShapeLayer *cornerMaskLayer = [CAShapeLayer layer];
      
      
      UIColor * borderColor = color;
      roundedCornerLayer.strokeColor = borderColor.CGColor;
      
      // Begin corner path
      UIBezierPath *roundedCorners = [UIBezierPath bezierPath];
      
      [roundedCorners moveToPoint:CGPointMake(0, objectView.frame.size.height - bottomLeftRadius)];
      [roundedCorners addLineToPoint:CGPointMake(0, 0 + topLeftRadius)];
      
      
      
      if(topLeftRadius != 0){
          [roundedCorners addArcWithCenter:CGPointMake(topLeftRadius, topLeftRadius)
                                radius:radius
                            startAngle:DEGREES_TO_RADIANS(180)
                              endAngle:DEGREES_TO_RADIANS(270)
                             clockwise:YES];
      
      
      
      }
      
      
      [roundedCorners addLineToPoint:CGPointMake(objectView.frame.size.width - topRightRadius, 0)];
      
      if(topRightRadius != 0){
          [roundedCorners addArcWithCenter:CGPointMake(objectView.frame.size.width - topRightRadius, 0 + topRightRadius)
                                radius:radius
                            startAngle:DEGREES_TO_RADIANS(270)
                              endAngle:DEGREES_TO_RADIANS(360)
                             clockwise:YES];
      }
      
      [roundedCorners addLineToPoint:CGPointMake(objectView.frame.size.width, objectView.frame.size.height - bottomRightRadius)];
      
      if(bottomRightRadius != 0){
          [roundedCorners addArcWithCenter:CGPointMake(objectView.frame.size.width - bottomRightRadius, objectView.frame.size.height - bottomRightRadius)
                                radius:radius
                            startAngle:DEGREES_TO_RADIANS(0)
                              endAngle:DEGREES_TO_RADIANS(90)
                             clockwise:YES];
      }
      
      [roundedCorners addLineToPoint:CGPointMake(0 + bottomLeftRadius, objectView.frame.size.height)];
      
      if(bottomLeftRadius != 0){
          [roundedCorners addArcWithCenter:CGPointMake(0 + bottomLeftRadius, objectView.frame.size.height - bottomLeftRadius)
                                radius:radius
                            startAngle:DEGREES_TO_RADIANS(90)
                              endAngle:DEGREES_TO_RADIANS(180)
                             clockwise:YES];
      }
      
      
      roundedCornerLayer.path = roundedCorners.CGPath;
      roundedCornerLayer.fillColor = [UIColor clearColor].CGColor;
      roundedCornerLayer.lineWidth = borderWidth;
      
      [cornerMaskLayer setPath:roundedCorners.CGPath];
      objectView.layer.mask = cornerMaskLayer;
      [objectView.layer addSublayer:roundedCornerLayer];
      

      }

1 个答案:

答案 0 :(得分:0)

roundedCornerLayer.lineWidth = borderWidth;设置路径的宽度。我不确定你是否希望相同的路径具有不同的宽度或不同宽度的不同路径。

如果您想在一个路径中使用不同的宽度,则必须将其分成单独的子路径,并且每个路径都有不同的lineWidth

如果你想拥有不同的路径,那么只需在调用方法时更改width的值。
我错过了什么吗?