iOS笔记之使用贝塞尔曲线给View/Layer添加圆角
2017-07-19 本文已影响36人
dragonYao
1、添加左上、右下圆角
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(10, 80, 150, 70)];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
//添加圆角
UIBezierPath *cornerPath = [UIBezierPath bezierPathWithRoundedRect:redView.bounds byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerBottomRight) cornerRadii:CGSizeMake(35, 35)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = redView.bounds;
maskLayer.path = cornerPath.CGPath;
redView.layer.mask = maskLayer;
2、添加右上、左下圆角
UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(10, 200, 150, 70)];
greenView.backgroundColor = [UIColor greenColor];
[self.view addSubview:greenView];
//添加圆角
UIBezierPath *cornerPath1 = [UIBezierPath bezierPathWithRoundedRect:greenView.bounds byRoundingCorners:(UIRectCornerTopRight|UIRectCornerBottomLeft) cornerRadii:CGSizeMake(35, 35)];
CAShapeLayer *maskLayer1 = [CAShapeLayer layer];
maskLayer1.frame = greenView.bounds;
maskLayer1.path = cornerPath1.CGPath;
greenView.layer.mask = maskLayer1;
3、使View成为圆(如:用户的头像)
UIView *circleView = [[UIView alloc] initWithFrame:CGRectMake(200, 150, 100, 100)];
circleView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:circleView];
//添加圆角
UIBezierPath *circlePath = [UIBezierPath bezierPathWithRoundedRect:circleView.bounds byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerBottomRight|UIRectCornerTopRight|UIRectCornerBottomLeft) cornerRadii:CGSizeMake(50, 50)];
CAShapeLayer *circleLayer = [CAShapeLayer layer];
circleLayer.frame = circleView.bounds;
circleLayer.path = circlePath.CGPath;
circleView.layer.mask = circleLayer;
效果图如下 👇
![](https://img.haomeiwen.com/i1749065/d6011a4659e748fc.png)