2016-07-25 63 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