• 在iOS开发中,我们经常会遇到设置圆角的问题, 以下是几种设置圆角的方法:

第一种方法: 通过设置layer的属性

代码:

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"willwang"]];

//只需要设置layer层的两个属性
//设置圆角
imageView.layer.cornerRadius = //将多余的部分切掉
imageView.layer.masksToBounds = YES; [self.view addSubview:imageView];
  • 这个实现方法里maskToBounds会触发离屏渲染(offscreen rendering),GPU在当前屏幕缓冲区外新开辟一个渲染缓冲区进行工作,也就是离屏渲染,这会给我们带来额外的性能损耗,如果这样的圆角操作达到一定数量,会触发缓冲区的频繁合并和上下文的的频繁切换,性能的代价会宏观地表现在用户体验上<掉帧>不建议使用.

第二种方法: 使用贝塞尔曲线UIBezierPath和Core Graphics框架画出一个圆角

效果图:

代码:

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(, , , )];

 imageView.image = [UIImage imageNamed:@"willwang"];

    //开始对imageView进行画图
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 0.0); //使用贝塞尔曲线画出一个圆形图
[[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addClip]; [imageView drawRect:imageView.bounds]; imageView.image = UIGraphicsGetImageFromCurrentImageContext(); //结束画图
UIGraphicsEndImageContext(); [self.view addSubview:imageView];
  • UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale) 各参数含义:
  • size —— 新创建的位图上下文的大小
  • opaque —— 透明开关,如果图形完全不用透明,设置为YES以优化位图的存储。
  • scale —— 缩放因子 iPhone 4是2.0,其他是1.0。虽然这里可以用[UIScreen mainScreen].scale来获取,但实际上设为0后,系统就会自动设置正确的比例

第三种方法: 使用Core Graphics框架画出一个圆角

代码:

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(, , , )];

    imageView.image = [UIImage imageNamed:@"willwang"];

    //开始对imageView进行画图
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0); // 获得图形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext(); // 设置一个范围
CGRect rect = CGRectMake(, , imageView.frame.size.width, imageView.frame.size.height); // 根据一个rect创建一个椭圆
CGContextAddEllipseInRect(ctx, rect); // 裁剪
CGContextClip(ctx); // 将原照片画到图形上下文
[imageView.image drawInRect:rect]; // 从上下文上获取剪裁后的照片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); // 关闭上下文
UIGraphicsEndImageContext(); imageView.image = newImage; [self.view addSubview:imageView];

第四种方法: 使用CAShapeLayer和UIBezierPath设置圆角

代码:

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(, , , )];

    imageView.image = [UIImage imageNamed:@"willwang"];

    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];

第四种方法延伸 指定需要成为圆角的角

方法:

+ (UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect
byRoundingCorners:(UIRectCorner)corners
cornerRadii:(CGSize)cornerRadii
corners参数指定了要成为圆角的角, 枚举类型如下:
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
UIRectCornerTopLeft = <&lt; ,
UIRectCornerTopRight = <&lt; ,
UIRectCornerBottomLeft = <&lt; ,
UIRectCornerBottomRight = <&lt; ,
UIRectCornerAllCorners = ~0UL
};

效果图:

代码:

//设置视图位置和大小
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(, , , )]; //设置背景颜色
myView.backgroundColor = [UIColor redColor]; //添加
[self.view addSubview:myView]; //绘制圆角 要设置的圆角 使用“|”来组合
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:myView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(, )]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; //设置大小
maskLayer.frame = myView.bounds; //设置图形样子
maskLayer.path = maskPath.CGPath; myView.layer.mask = maskLayer; UILabel *label = [[UILabel alloc]init]; //添加文字
label.text = @"willwang"; //文字颜色
label.textColor = [UIColor whiteColor]; [myView addSubview: label]; //自动布局
[label mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(myView);
}];

第五种方法 在storyboard或xib中设置

iOS设置圆角的方法及指定圆角的位置的更多相关文章

  1. 使用Hbuilder开发IOS应用上架审核提示请指定用户在位置许可模式警报中使用位置的预定用途。

    使用Hbuilder开发IOS应用时,遇到上架App被拒的问题,被拒原因: 你的应用程序使用位置服务,但并没有按照iOS人机界面指南中的要求,在位置模式警报中阐明它的用途. 要解决此问题,请指定用户在 ...

  2. 蜗牛爱课 -- iOS 设置UIButton的字体的大小、显示位置、大小

    /设置按钮上的自体的大小 //[btn setFont: [UIFont systemFontSize: 14.0]];    //这种可以用来设置字体的大小,但是可能会在将来的SDK版本中去除改方法 ...

  3. 【转载】C#中使用List集合的Insert方法在指定位置插入数据

    在C#的List集合等数据类型变量中,我们可以使用List集合的Insert方法在指定的索引位置插入一个新数据,例如指定在List集合的第一个位置写入一个新数据或者在List集合的中间某个位置插入个新 ...

  4. 指定程序集的位置 | Microsoft Docs

    原文:指定程序集的位置 | Microsoft Docs 指定程序集的位置Specifying an Assembly's Location 2017/03/30 作者 使用<b a s e & ...

  5. iOS设置圆角的四种方法

    小小圆角问题,正常情况下,我们不需要过多关心,但当屏幕内比较多的时候,还是有必要了解下性能问题的 一.设置CALayer的cornerRadius 这是最常用的,也是最简单的. cornerRadiu ...

  6. php imagick设置图片圆角的方法

    php imagick设置图片圆角的方法 <pre>header('Content-Type: image/png'); $image = new Imagick('http://stat ...

  7. ios开发中全局变量设置和调用方法

    ios开发中,全局变量设置和调用方法如下:在AppDelegate.h文件中设置全局变量:@interface ***AppDelegate{NSString *myName;}@property ( ...

  8. iOS与HTML5交互方法总结(转)

    今天小编在找技术文章的时候,发现这样一个标题:iOS与HTML5交互方法总结,怎么看着这么熟悉呢?   还以为是刚哥用了别的文章,点进去一看,原来是刚哥自己写的文章,他们转载的,而且还上了Dev St ...

  9. iOS与HTML5交互方法总结(修正)

    摘要 看了不少别人写的博客或者论坛,关于iOS与HTML5交互方法大概主要有5种方式: 1. 利用WKWebView进行交互(系统API) 2. 利用UIWebView进行交互(系统API) 3. 苹 ...

随机推荐

  1. NPOI导出excel(带图片)

    近期项目中用到Excel导出功能,之前都是用普通的office组件导出的方法,今天尝试用下NPOI,故作此文以备日后查阅. 1.NPOI官网http://npoi.codeplex.com/,下载最新 ...

  2. Bootstrap文本排版基础--Bootsrap

    1.排版前的基础 (1)移动设备优先 <meta name="viewport" content="width=device-width, initial-scal ...

  3. .NET Framework基本概念

    http://blog.csdn.net/T573029173/article/details/41730101 .NET是微软的新一代技术平台.对技术人员来说,想真正了解什么是.NET,须先了解.N ...

  4. IDEA搭建SpringMVC+Mybatis+Mysql+Maven框架

    相关环境 Intellij IDEA Ultimate Tomcat JDK MySql 5.6(win32/win64) Maven (可使用Intellij IDEA自带的) 搭建步骤 创建项目工 ...

  5. python第四课——线程、进程、协程

    面试or笔试题:简述线程.进程.协程之间的关系? 内容概要 1.进程与线程优.缺点的比较 2.适用情况 3.线程 线程的创建 setDaemon join event RLock 队列 4.进程 创建 ...

  6. Acitiviti笔记(一)

    一.核心组件 ProcessEngine:流程引擎的抽象,对于开发者来说,它是我们使用Activiti的facade,通过它可以获得我们需要的一切服务.类似于一个容器工厂,来保存创建的ProcessE ...

  7. sublime text3 开发必备插件

    1,Package Control 通俗易懂地说,这个是你在完成安装SublimeText后必须安装的东西.你问为什么?因为有了这个特殊的"插件包",你可以很容易地安装.升级.删除 ...

  8. switchhost -- 切换host的工具

    https://github.com/oldj/SwitchHosts/downloads 下载链接: 1,290 downloads SwitchHosts! _v0.2.2.1790.dmg - ...

  9. CSS3新特性汇总

    新增选择器 p:nth-child(n){color: rgba(255, 0, 0, 0.75)} 弹性盒模型 display: flex; 多列布局 column-count: 5; 媒体查询 @ ...

  10. Linux下Git安装、配置

    安装 首先查看下有没有安装过 输入 git,出现以下的,就说明安装过了. 否则, 执行命令:sudo apt-get install git    进行安装 安装好之后,还需要执行命令: git co ...