2016-07-25 131 views
0
-(void)setTopRightCornerWithRadious:(CGFloat)radious View:(UIView*)vw 
{ 
    UIGraphicsGetCurrentContext(); 
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:vw.bounds byRoundingCorners:UIRectCornerTopRight cornerRadii:CGSizeMake(radious, radious)]; 
    [maskPath closePath]; 
    CAShapeLayer *maskLayer = [CAShapeLayer layer]; 
    maskLayer.frame = vw.bounds; 
    maskLayer.path = maskPath.CGPath; 
    vw.layer.mask=maskLayer; 

    if (vw.layer.borderColor) { 
     UIColor *color=[UIColor colorWithCGColor:vw.layer.borderColor]; 
     [color setStroke]; 
     maskLayer.accessibilityPath.lineWidth=1.0f; 
     [maskLayer.accessibilityPath stroke]; 
    } 
} 
-(void)setAllBorderForView:(UIView*)vw Color:(UIColor*)color Thickness:(CGFloat)thick 
{ 
    if (vw) { 
     vw.layer.borderWidth=thick; 
     vw.layer.borderColor=color.CGColor; 
    } 
} 

enter image description here周圍畫一個多邊形

邊境謹通過這兩個按鈕包圍的邊框。我用CAShapeLayer和UIBezierPath嘗試過很多次,但都失敗了,可能我錯過了一些東西。他們中的一些人使用UIView解決了這個問題,但我不想那麼做。我只想通過使用CAShapeLayer和/或UIBezierPath來解決問題。

這是我的smaple代碼......是我的錯?一開始我設置邊界,然後我試圖設置角落。幾乎沒有邊界顏色存在或可能不存在。

+0

是dooesn't看起來像一個多邊形。你需要每個按鈕的邊框還是兩個按鈕的邊框? –

+0

@TejaNandamuri有兩個單獨的按鈕,我想單獨放置由它們包圍的邊框,即邊框將用於每個按鈕。由於我不能提到它是正方形還是矩形,所以我將其稱爲多邊形。可能是我錯了,請糾正我。在此先感謝:) – SAM

+0

如果你想在邊界和按鈕之間有空間,最簡單的方法是添加兩條貝塞爾路徑。其中一個用於按鈕,另一個用於邊框,您可以將按鈕添加到按鈕貝塞爾路徑。 –

回答

0

在您的自定義按鈕設置此:

UIBezierPath outerPAth= [UIBezierPath bezirePath]; 
    [[UIColor WhiteColor] setStroke]; 
    outlinePath.lineWidth=5.0; 
    [outlinePath stroke]; 
0

如果你只是需要有按鈕周圍的邊框,添加行程按鈕貝塞爾路徑。

enter image description here

- (void)drawRect: (CGRect)frame 
{ 
UIBezierPath* rectangle2Path = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(x,y,widht,height) byRoundingCorners: UIRectCornerTopRight cornerRadii: CGSizeMake(17.25, 17.25)]; 
[rectangle2Path closePath]; 
[UIColor.grayColor setFill]; 
[rectangle2Path fill]; 
[UIColor.redColor setStroke]; 
rectangle2Path.lineWidth = 1; 
[rectangle2Path stroke]; 
} 

,或者如果你想有邊框和按鍵貝塞爾路徑之間的空間,那麼你應該增加兩個貝塞爾路徑。一個用於按鈕,另一個用於邊框。

enter image description here

- (void)drawRect: (CGRect)frame 
{ 

    UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(CGRectGetMinX(frame), CGRectGetMinY(frame), floor((CGRectGetWidth(frame)) * 1.00000 + 0.5), floor((CGRectGetHeight(frame)) * 1.00000 + 0.5)) byRoundingCorners: UIRectCornerTopRight cornerRadii: CGSizeMake(28, 28)]; 
    [rectanglePath closePath]; 
    [UIColor.redColor setStroke]; 
    rectanglePath.lineWidth = 1; 
    [rectanglePath stroke]; 


    UIBezierPath* rectangle2Path = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(CGRectGetMinX(frame) + 7, CGRectGetMinY(frame) + 8, 103, 62) byRoundingCorners: UIRectCornerTopRight cornerRadii: CGSizeMake(26, 26)]; 
    [rectangle2Path closePath]; 
    [UIColor.grayColor setFill]; 
    [rectangle2Path fill]; 
} 
+0

我已更新我的問題。其實情況就像上面那樣。我只能調用兩種方法,我必須使用邊框顏色來製作角落。 – SAM