美文网首页
iOS控件圆角

iOS控件圆角

作者: 汤姆杰瑞 | 来源:发表于2020-12-18 10:24 被阅读0次

在开发过程常遇到控件圆角的设计;我们常用的方式是设置layer属性如下:

self.imageView.layer.cornerRadius = 5;
self.imageView.layer.masksToBounds = YES;

这种处理的渲染机制是GPU在当前屏幕缓冲区外新开辟一个渲染缓冲区进行工作,也就是离屏渲染。如果圆角操作达到一定数量,会触发缓冲区的频繁合并和上下文频繁切换,性能的代价会宏观地表现在用户体验上——掉帧。

方案1:使用贝塞尔曲线UIBezierPath和Core Graphics框架画出一个圆角

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
imageView.image = [UIImage imageNamed:@"imagename"];
//开始对imageView进行画图
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);
//使用贝塞尔曲线画出一个圆形图
[[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addClip];
[imageView drawRect:imageView.bounds];
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.view addSubview:imageView];

方案2:使用CAShapeLayer和UIBezierPath设置圆角

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100,100,100,100)];
imageView.image = [UIImage imageNamed:@"imagename"];
UIBezierPath *maskPath=[UIBezierPath bezierPathWithRoundedRect:imageView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:imageView.bounds.size];
CAShapeLayer *maskLayer=[[CAShapeLayer alloc]init];
maskLayer.frame=imageView.bounds;
maskLayer.path=maskPath.CGPath;
imageView.layer.mask=maskLayer;
[self.view addSubview:imageView];

转发:https://www.jianshu.com/p/b4b002f8f1e7

相关文章

  • iOS Objective-C Popover简单圆角阴影气泡实

    QPopover 介绍 QPopover, iOS圆角阴影气泡控件,支持圆角阴影、箭头只支持上下方向,只需提供一张...

  • iOS控件圆角

    在开发过程常遇到控件圆角的设计;我们常用的方式是设置layer属性如下: 这种处理的渲染机制是GPU在当前屏幕缓冲...

  • TabelView图片圆角的性能优化测试

    导语 控件切圆角是我们 iOS 开发中经常要做的事情,如果仅仅是单一控件进行切圆角的话,对性能的影响并不大,毕竟现...

  • iOS 控件切圆角

    在此处看到的原文 控件如果切四个圆角的话,调用下面的代码 如果只是切某几个角的话,调用以下的代码

  • iOS 控件圆角图片

  • ios设置控件圆角

    0.最简单实用,不影响帧数的设置全圆角的方法,是自定义一个类继承于UIimageview,在它上面在加一个imag...

  • new learning----- Cut All Corner

    最近接触了个新项目,写了几个new界面。关于控件切圆角设置的,有几点想法想谈下。。。。 iOS切圆角的方式 本篇只...

  • iOS10 给控件切圆角.

    开宗明义: iOS10给控件切圆角的代码需要写在layoutSubviews方法中. 问题. 升级iOS10及Xc...

  • iOS 设置UI控件圆角

    一: 设置UIView上方圆角或者下方圆角 //设置UITableView section 圆角 设置好以后就这样了

  • iOS控件指定圆角设置

    直接上代码 指定哪个角设置圆角的枚举 对你有帮助记得点小心心❤️

网友评论

      本文标题:iOS控件圆角

      本文链接:https://www.haomeiwen.com/subject/ffbzgktx.html