【问题标题】:Draw border around a polygon在多边形周围绘制边框
【发布时间】:2016-07-25 13:01:55
【问题描述】:
    -(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;
    }
}

我想画一个被这两个按钮包围的边框。我用 CAShapeLayer 和 UIBezierPath 尝试了很多次,但都失败了,可能是我错过了一些东西。他们中的一些人使用 UIView 解决了这个问题,但我不希望这样。我只想通过使用 CAShapeLayer 和/或 UIBezierPath 来解决问题。

这是我的简单代码...我的错在哪里????一开始我设置了边界,然后我尝试设置角落。边框颜色可能存在也可能不存在。

【问题讨论】:

  • 看起来不像多边形。您需要为这些按钮中的每一个设置一个边框还是为两个按钮设置一个边框?
  • @TejaNandamuri 有两个单独的按钮,我想分别放置由它们包围的边框,即每个按钮都有边框。而且由于我无法提及它是正方形还是矩形,所以我将其称为多边形。可能是我错了,请纠正我。在此先感谢:)
  • 如果你想在边框和按钮之间留出空间,最简单的方法是添加两个贝塞尔路径。一个用于按钮,一个用于边框,否则您可以将描边添加到按钮贝塞尔路径。

标签: ios objective-c uibezierpath cashapelayer


【解决方案1】:

如果您只需要在按钮周围设置边框,请在按钮贝塞尔路径中添加描边。

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

或者如果你想在边框和按钮贝塞尔路径之间留出空间,那么你应该添加两个贝塞尔路径。一个用于按钮,另一个用于边框。

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

【讨论】:

  • 我已经更新了我的问题。其实情况如上。我只能调用两种方法应用,我必须将角与边框颜色一起制作。
【解决方案2】:

在您的自定义按钮类中设置:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    相关资源
    最近更新 更多