• 在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. DataRow和DataRowView的区别

    可以将DataView同数据库的视图类比,不过有点不同,数据库的视图可以跨表建立视图,DataView则只能对某一个DataTable建立视图. DataView一般通过DataTable.Defau ...

  2. DevOps之主机

    唠叨话 关于德语噢屁事的知识点,仅提供专业性的精华汇总,具体知识点细节,参考教程网址,如需帮助,请留言. 主机(Host) 关于主机,知识与技能的层次(知道.理解.运用),理论与实践的方面(原理.技术 ...

  3. Maven Install指令构建时出现找不到符号

    检查引用的JRE编译的版本,可能由于JRE编译版本太低导致的  

  4. [extjs(2)] extjs第一个组件treepanel

    刚刚在接触extjs这个前段插件,由于公司是用这个来做前段的,所以有必要花点时间来掌握一下,下面是我自己的非常浅的学习总结,后期会慢慢添加的!! 一.TreePanel基本配置参数: animate: ...

  5. Promise对象解读

    首先强调的是"Promise"是对象,也就是说与其他JavaScript对象的用法,没有什么两样:其次,它起到代理作用(proxy),充当异步操作与回调函数之间的中介.它使得异步操 ...

  6. JAVA基础知识总结:六

    一.不定长参数 1.语法:数据类型... 变量名称 使用注意事项:a.不定长参数就相当于是一个数组 b.不定长参数只能出现在参数列表的最后面 c.一个函数的参数列表中只能出现一次不定长参数 d.对于不 ...

  7. 如何线上部署node.js项目

    来源:http://blog.csdn.net/chenlinIT/article/details/73343793 前言 最近工作不是很忙,在空闲时间学习用node+express搭建自己的个人博客 ...

  8. js判断值为null

    今天在做项目的时候,犯了一个着实不应该的错误,拿到data为null,然后判断如果为null执行A,否则执行B 我错误的代码是 if(data===null){ A; }else{ B; } 怎么调试 ...

  9. linux学习(八)chmod、chown、umask、lsattr、chattr

    一.权限位 权限位分为两个部分,第一个部分是谁的权限,第二部分是权限是多少.其中第一个部分一般分为:用户,用户组,其他用户.第二部分分为r:读权限,w:写权限,x:执行权限.可读,可写,可执行的权限, ...

  10. 数据结构中,几种树的结构表示方法(C语言实现)

    //***************************************** //树的多种结构定义 //***************************************** # ...