ios view 圆角

我们给一个View设置圆角一般都是用如下代码

// 设置layer超出父图层的部分剪切掉
self.cornerView.layer.masksToBounds = YES;
// 设置圆角半径,若imageView1为正方形,设置圆角半径为边长的一半可实现圆效果
self.cornerView.layer.cornerRadius = 20.0;

但是如果需求是只要左上和右上圆角以上方法肯定不能实现,直接上代码

UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:self.cornerView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:(CGSize){20.0}];
 CAShapeLayer *shapeLayer = [CAShapeLayer layer];
// 设置绘制路径
shapeLayer.path = bezierPath.CGPath;
// 将shapeLayer设置为cornerView的layer的mask
self.cornerView.layer.mask = shapeLayer;

实现四个圆角

// 绘制4个角,
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:self.cornerView.bounds cornerRadius:20.0];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
// 设置绘制路径
shapeLayer.path = bezierPath.CGPath;
self.cornerView.layer.mask = shapeLayer;

你可能感兴趣的:(ios view 圆角)