iOS_UIBezierPath与CAShapeLayer创建圆角UIView

今天的这篇文章将会介绍如何用代码实现下面的效果...
  我们常用CALayer的cornerRadius属性来构造出一个视图的圆角,但这种方法不能单独定义某一个角是圆角.

iOS_UIBezierPath与CAShapeLayer创建圆角UIView_第1张图片
Snip20170512_4.png
CAShaperLayer与UIBezierPath构造图层圆角

虽然使用CAShapeLayer类需要更多的工作,但是它可以单独指定某一个角是圆角.下面为具体实现:

    UIView *viewRadi = [UIView new];
    viewRadi.frame = CGRectMake(50, 50, 100, 100);
    [self.view addSubview:viewRadi];

    CGRect rect = CGRectMake(0, 0, 100, 100);
    CGSize radi = CGSizeMake(20, 20);
    UIRectCorner corners = UIRectCornerTopRight | UIRectCornerBottomRight | UIRectCornerBottomLeft;
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:radi];
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.strokeColor = [UIColor redColor].CGColor;
    shapeLayer.fillColor = [UIColor orangeColor].CGColor;
    shapeLayer.lineWidth = 3;
    shapeLayer.lineJoin = kCALineJoinRound;
    shapeLayer.lineCap = kCALineCapRound;
    shapeLayer.path = path.CGPath;
    [viewRadi.layer addSublayer:shapeLayer];





  模拟器运行如下:
iOS_UIBezierPath与CAShapeLayer创建圆角UIView_第2张图片
Snip20170512_5.png

你可能感兴趣的:(iOS_UIBezierPath与CAShapeLayer创建圆角UIView)