2014-10-17 38 views
0

我試圖構建一個設計應用程序,其中一個功能允許用戶預覽各種資產的不同類型的自定義邊界。具有多行寬度的UIBezierPath路徑

我想要使用UIBezierPath來繪製這些邊框。

眼下系統將支持:

  1. 各邊境半徑(任何從0 - >其他地方計算的上限)
  2. 不同顏色的整個邊界

我想執行:

  1. 具有不同邊框線寬的能力。

我認爲我應該能夠通過繪製每個不同寬度的路徑來實現這一目標,但我無法弄清楚如何將其從路徑更改爲路徑。誰能幫我嗎?

謝謝。

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]; 

}

回答

0

roundedCornerLayer.lineWidth = borderWidth;設置路徑的寬度。我不確定您是否希望相同的路徑具有不同的寬度或具有不同寬度的不同路徑。

如果您想在一個路徑中使用不同的寬度,那麼您必須將其分成不同的子路徑,並且每個子路徑都會有不同的lineWidth

如果你想有不同的路徑,那麼當你調用該方法時,只需更改width的值。
我錯過了什麼嗎?

+0

嘿謝謝你回到我身邊。如果可能的話,我希望能夠在四邊的每一邊上以不同的線寬進行此操作。子路徑如何工作? – RTP33 2014-10-17 23:39:25

+0

由於您實際上並未繪製路徑,而是將它們添加爲子圖層,因此我只想爲每一面創建不同的路徑並將它們添加到圖層中。 – Yariv 2014-10-18 00:07:37

+0

但它是一個面具,所以它裁剪視圖。任何代碼示例? – RTP33 2014-10-18 12:08:23