iOS设置圆形(圆角)UIView、UIButton的简单方法
2016-03-21 16:40
537 查看
有时候需要快速的创建一些圆形的UIView,UIButton之类的,如果有时间自己封装控件,使用CG绘制当然很好,但是如果没有时间,那么就使用CALayer的特性来做吧。
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; //只需要设置layer层的两个属性 //设置圆角 imageView.layer.cornerRadius = imageView.frame.size.width / 2; //将多余的部分切掉 imageView.layer.masksToBounds = YES; [self.view addSubview:imageView];----------------------------------------------------------------------------------------------------------------------------------------------------------------
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];imageView.image = [UIImage imageNamed:@"1"];//开始对imageView进行画图UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, [UIScreen mainScreen].scale);//使用贝塞尔曲线画出一个圆形图[[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addClip];[imageView drawRect:imageView.bounds];imageView.image = UIGraphicsGetImageFromCurrentImageContext();//结束画图UIGraphicsEndImageContext();[self.view addSubview:imageView];-----------------------------------------------------------------------------------------------------------------------------------------------------------------第三种方法,性能最好写,记得导入<AVFoundation/AVFoundation.h>
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];imageView.image = [UIImage imageNamed:@"1"];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];}这三种方法中第三种最好,对内存的消耗最少啊,而且渲染快速。 iOS© 著作权归作者所有举报文章如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!发个红包喜欢 139
相关文章推荐
- Generic Interfaces (C# Programming Guide)
- [ActionScript&Flex] FlashBuilder编译条件之如何屏蔽调试代码
- soapui脚本中,参数内容为xml文件时候的处理
- UIButton使用误区
- String Stringbuffer Stringbuilder区别
- UIPickView手动选择时动画
- Servlet的Request.getInputStream()只能读取一次
- ndarray.any()--ValueError: 'axis' entry is out of bounds
- iOS 自定义UITableViewCell上添加UIButton按钮如何确定点击了哪一行,代码块实现
- 在使用cocoapods的过程中出现 Use the `$(inherited)` flag, or Remove the build settings from the target.
- HBuilder真机联调iPhone手机设备问题
- 基于easyui开发Web版Activiti流程定制器详解(六)——Draw2d的扩展(三)
- 基于easyui开发Web版Activiti流程定制器详解(六)——Draw2d详解(二)
- Bluetooth
- UICollectionView 与屏幕旋转 问题
- blueGreen develop , 敏捷开发
- GPUImage框架导入到xcode工程
- codeforces644B. Processing Queries (模拟)
- 基于easyui开发Web版Activiti流程定制器详解(五)——Draw2d详解(一)
- maven创建项目报Could not calculate build plan...