iOS如何删除细边框线然后使用layer.cornerRadius

时间:2015-03-11 01:39:46

标签: ios core-graphics layer rounded-corners

我总是使用简单的方法来获取视角圆角

+ (void)setRoundedCornersByView:(UIView*) givenView roundAngle:(int)roundAngle borderWidth:(double)borderWidth borderColor:(UIColor*)borderColor alphaBorder:(double)alphaBorder {
    givenView.layer.cornerRadius = roundAngle;
    givenView.layer.borderColor = [[borderColor colorWithAlphaComponent:alphaBorder] CGColor];
    givenView.layer.borderWidth = borderWidth;
    givenView.layer.masksToBounds = YES;
}

但是现在我在圆线周围有一个细边框,它是一条细线,颜色像圆角的背景颜色

enter image description here

如何在不使用onDraw的情况下删除它,因为它无法做到 - 因为它意味着我必须覆盖所有需要圆角的iOS控件。

另外,请尝试使用

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bound byRoundingCorners:corners cornerRadii:cornerRadii];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = givenView.bounds;
maskLayer.path = maskPath.CGPath;
givenView.layer.mask = maskLayer;

但是,正如你所看到的,它没有完全圆润

enter image description here

1 个答案:

答案 0 :(得分:1)

显然,在角点绘制剪辑后,图层边界不足以覆盖整个图像。因此,您可以稍微扩大图层的边界以覆盖视图的图像。像这样:

+ (void)setRoundedCornersByView:(UIView *)givenView roundAngle:(int)roundAngle borderWidth:(double)borderWidth borderColor:(UIColor *)borderColor alphaBorder:(double)alphaBorder
{
  CGFloat offset = 1.f; // .5f is also good enough
  givenView.layer.cornerRadius = roundAngle + offset;
  givenView.layer.borderColor = [[borderColor colorWithAlphaComponent:alphaBorder] CGColor];
  givenView.layer.borderWidth = borderWidth + offset;
  givenView.layer.masksToBounds = YES;

  [givenView.layer setBounds:CGRectMake(-offset,
                                        -offset,
                                        CGRectGetWidth(givenView.frame)  + offset * 2.f,
                                        CGRectGetHeight(givenView.frame) + offset * 2.f)];
}