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