2014-03-27 98 views
0

我将角半径应用于UIView,即UIRectCornerTopLeftUIRectCornerTopRight。当我申请这个时,边界在角落里消失了。如何避免这种情况?如何制作带有可选圆角和边框的UIView?

这是我如何适用边界UIView

[self.middleView addRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight withRadii:CGSizeMake(4, 4)]; 
self.middleView.layer.borderWidth = 0.5f; 
self.middleView.layer.borderColor = [[UIColor colorWith8BitRed:0 green:0 blue:0 alpha:0.25] 

这是我使用的应用可选圆角的类别:

#import "UIView+Roundify.h" 

    @implementation UIView (Roundify) 
    - (void)addRoundedCorners:(UIRectCorner)corners withRadii:(CGSize)radii { 
    CALayer *tMaskLayer = [self maskForRoundedCorners:corners withRadii:radii]; 
    self.layer.mask = tMaskLayer; 
    } 

    - (CALayer*)maskForRoundedCorners:(UIRectCorner)corners withRadii:(CGSize)radii { 
    CAShapeLayer *maskLayer = [CAShapeLayer layer]; 
    maskLayer.frame = self.bounds; 

    UIBezierPath *roundedPath = [UIBezierPath bezierPathWithRoundedRect: 
          maskLayer.bounds byRoundingCorners:corners cornerRadii:radii]; 
    maskLayer.fillColor = [[UIColor whiteColor] CGColor]; 
    maskLayer.backgroundColor = [[UIColor clearColor] CGColor]; 
    maskLayer.path = [roundedPath CGPath]; 

    return maskLayer; 
} 
+0

你的意思是可选的圆角和边框? –

+0

@icodes尝试将'strokeColor'设置为'CAShapeLayer * maskLayer' – Akhilrajtr

+0

请参阅此处的答案:http://stackoverflow.com/questions/12756928/cashapelayer-with-border-and-fill-color-and-rounding – n00bProgrammer

回答

1

除非有我们”一些具体要求不知道,如果你所要做的只是绕过角落并有边界,那么贝塞尔路径和掩码是不必要的。我通常会这样做: myView.layer.borderWidth = 2; myView.layer.cornerRadius = 5;

难道你只是想要四舍五入的顶角,你需要不使用层舍入?如果是这样,为什么不使用它,然后覆盖一个薄的视图来覆盖底部位?有点烦琐,但是我发现越是靠标准控件来绘制自己,而不是进入核心图形就越好。

编辑:好的,因为它需要有底角不是圆的,怎么样,如果你有2子视图上的UIView类别:1与4圆角和另一奠定过顶(个体经营bringSubviewToFront)它简单地覆盖了带有非圆形条带的圆形视图的“页脚”,即具有等于圆角半径的等宽和小高度的视图。如果你有一个纯色背景,那么就让两个子视图都一样;如果您有一些纹理或图像背景,请使它们都透明,并将纹理/图像放在超级视图(使用您类别的特定布局方法的父视图)上。最后,将边框放在同一个超级视图上。应该工作,让我知道你的想法。

+0

是的,我的要求是特定的。我在我的应用程序的几个视图中使用了这个类别,只有顶部两个角被边框圆化。我可以得到圆角但不能将边框应用到这些边框 – icodes

+0

请参阅已编辑的答案 – bobsmells

4

下面的代码尝试工作

您的看法要圆左上和TopRight

UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(50, 100, 100, 100)]; 
    [view1 setBackgroundColor:[UIColor grayColor]]; 
    [self.view addSubview:view1]; 

组边角如下代码

UIBezierPath *maskPath; 
maskPath = [UIBezierPath bezierPathWithRoundedRect:view1.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(5.0, 5.0)]; 

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; 
maskLayer.frame = self.view.bounds; 
maskLayer.path = maskPath.CGPath; 
view1.layer.mask = maskLayer; 

输出为:

enter image description here

+0

我可以得到圆角,但无法将边界应用于该视图。边界在角落消失 – icodes

1

找到了这段代码。实际上并没有尝试过,但似乎是你需要的。

- (void)drawDashedBorderAroundView:(UIView *)v { 

    //border definitions 
    CGFloat cornerRadius = 10; 
    CGFloat borderWidth = 2; 
    NSInteger dashPattern1 = 8; 
    NSInteger dashPattern2 = 8; 
    UIColor *lineColor = [UIColor orangeColor]; 

    //drawing 
    CGRect frame = v.bounds; 

    CAShapeLayer *_shapeLayer = [CAShapeLayer layer]; 

    //creating a path 
    CGMutablePathRef path = CGPathCreateMutable(); 

    //drawing a border around a view 
    CGPathMoveToPoint(path, NULL, 0, frame.size.height - cornerRadius); 
    CGPathAddLineToPoint(path, NULL, 0, cornerRadius); 
    CGPathAddArc(path, NULL, cornerRadius, cornerRadius, cornerRadius, M_PI, -M_PI_2, NO); 
    CGPathAddLineToPoint(path, NULL, frame.size.width - cornerRadius, 0); 
    CGPathAddArc(path, NULL, frame.size.width - cornerRadius, cornerRadius, cornerRadius, -M_PI_2, 0, NO); 
    CGPathAddLineToPoint(path, NULL, frame.size.width, frame.size.height - cornerRadius); 
    CGPathAddArc(path, NULL, frame.size.width - cornerRadius, frame.size.height - cornerRadius, cornerRadius, 0, M_PI_2, NO); 
    CGPathAddLineToPoint(path, NULL, cornerRadius, frame.size.height); 
    CGPathAddArc(path, NULL, cornerRadius, frame.size.height - cornerRadius, cornerRadius, M_PI_2, M_PI, NO); 

    //path is set as the _shapeLayer object's path 
    _shapeLayer.path = path; 
    CGPathRelease(path); 

    _shapeLayer.backgroundColor = [[UIColor clearColor] CGColor]; 
    _shapeLayer.frame = frame; 
    _shapeLayer.masksToBounds = NO; 
    [_shapeLayer setValue:[NSNumber numberWithBool:NO] forKey:@"isCircle"]; 
    _shapeLayer.fillColor = [[UIColor clearColor] CGColor]; 
    _shapeLayer.strokeColor = [lineColor CGColor]; 
    _shapeLayer.lineWidth = borderWidth; 
    _shapeLayer.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:dashPattern1], [NSNumber numberWithInt:dashPattern2], nil]; 
    _shapeLayer.lineCap = kCALineCapRound; 

    //_shapeLayer is added as a sublayer of the view, the border is visible 
    [v.layer addSublayer:_shapeLayer]; 
    v.layer.cornerRadius = cornerRadius; 
} 

这段代码增加了一条虚线,但您可以通过_shapeLayer.lineDashPattern修改该代码。