iOS 设置圆角
来源:互联网 发布:怎么查看淘宝信用等级 编辑:程序博客网 时间:2024/06/02 04:19
第一种方式:通过设置控件的layer属性
该方法是iOS实现圆角的方法中最简单的一种,比较影响性能
实现代码如下所示:
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];// 设置圆角 imageView.layer.cornerRadius = imageView.frame.size.width/2;// 将多余的部分剪切掉 imageView.layer.masksToBounds = YES; [self.view addSubview:imageView];如果要给控件设置边框可以设置layer的以下两个属性
// 设置边框 边框的宽度为10 imageView.layer.borderWidth = 10;// 设置边框的颜色 红色 imageView.layer.borderColor = [UIColor redColor].CGColor;
第二种方式:使用贝塞尔曲线UIBezierPath和Core Graphics框架画出一个圆角
实现代码如下所示:
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; imageView.image =[ UIImage imageNamed:@"logo1"]; // 开始对imageView进行画圆 UIGraphicsBeginImageContextWithOptions(imageView.frame.size, NO, 1.0); // 使用贝塞尔曲线画出一个圆形图 [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.bounds.size.width] addClip]; [imageView drawRect:imageView.bounds]; imageView.image = UIGraphicsGetImageFromCurrentImageContext(); //结束画图 UIGraphicsEndImageContext(); [self.view addSubview:imageView];第三种方式:使用CAShapeLayer和贝塞尔曲线UIBezierPath
实现代码如下所示:
UIImageView *Image = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; Image.image = [UIImage imageNamed:@"logo1"]; UIBezierPath *MaskPath = [UIBezierPath bezierPathWithRoundedRect:Image.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:Image.bounds.size]; CAShapeLayer *MaskLayer = [[CAShapeLayer alloc]init];// 这是大小 MaskLayer.frame = Image.bounds;// 设置图形的样子 MaskLayer.path = MaskPath.CGPath; Image.layer.mask = MaskLayer; [self.view addSubview:Image];
注意:第三种实现方式在使用的时候要导入AVFoundation.framework,然后导入头文件
#import <AVFoundation/AVFoundation.h>
以上三种实现圆角的方式中,第三种方式对内存的消耗最少。
在xib中对实现控件的圆角设置
第一步:选择要设置为圆角的控件。操作如下图所示:
第二步:添加KeyPath
设置完成之后如下图所示:
控件的KeyPath设置完成之后并不会立刻在Xib中显示为圆角,只有当程序运行的时候才会显示出圆角
0 0
- IOS 圆角设置
- iOS 设置圆角
- iOS 设置圆角
- iOS 设置圆角
- ios view 设置圆角
- IOS UILabel 设置圆角
- IOS--设置圆角Button
- ios 图片圆角设置
- iOS 设置图片圆角
- iOS图片圆角设置
- iOS 设置圆角(总结)
- ios代码设置圆角
- iOS 圆角矩形圆角设置
- 圆角图片的设置ios
- 圆角图片的设置ios
- 圆角图片的设置ios
- [IOS] 设置UIImagView显示圆角
- IOS 设置任意控件圆角边框
- android <merge>标签的使用
- Linux开发点点滴滴
- maven-通过maven命令创建maven项目
- 【安卓网络请求开源框架Volley源码解析系列】初识Volley及其基本用法
- Pomelo聊天服务器完美运行启动
- iOS 设置圆角
- mybatis使用like关键字方法
- DM8168 Encode Link执行流程
- opengl 简单滑动条类实现
- Android ListView显示不同布局Item
- 二维数组中的查找
- Java 高并发缓存与Guava Cache
- 搭建vsftpd服务器
- 6章 数组概述