iOS笔记之使用贝塞尔曲线给View/Layer添加圆角

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;

效果图如下 👇
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,237评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 15,460评论 4 61
  • 2017年,毕业生的工作着实比较难找。其实工作难找并不是突然发生的,这几年来,经济一直在急速的下滑,所有大公司都多...
    每天都有新实习阅读 330评论 0 0
  • 1碳水化合物 2维生素 3脂肪 4矿物质 5蛋白质 6水 五中食物组合 1谷物 2乳制品 3蔬菜 4蛋白质食物(肉...
    DoloresYu阅读 269评论 0 0
  • 青灯霜风弄瘦影, 不及掩面饰泪痕。 蜀山神女无恙否? 长揖暮光别此身。 目前笑靥娇仙娥, 无心凝窗影幢幢。
    灵魂刀手阅读 198评论 0 1

友情链接更多精彩内容