iOS设置圆形(圆角)UIView、UIButton的简单方法
来源:互联网 发布:windows hadoop2.7.3 编辑:程序博客网 时间:2024/06/05 01:42
有时候需要快速的创建一些圆形的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
© 著作权归作者所有
举报文章
0 0
- iOS设置圆形(圆角)UIView、UIButton的简单方法
- IOS设置圆形按钮的简单实现
- iOS UIButton的简单实用方法
- 将UIView的子类变为圆形的简单实现方式(圆形头像、圆形按钮)
- UIView,UIButton,UIImageView等设置圆角,设置阴影,设置边框的方法
- UIView,UIButton,UIImageView等设置圆角,设置阴影,设置边框的方法
- UIView,UIButton,UIImageView等设置圆角,设置阴影,设置边框的方法
- UIView,UIButton,UIImageView等视图设置圆角,设置阴影,设置边框的方法
- UIView,UIButton,UIImageView等设置圆角,设置阴影,设置边框的方法
- UIView,UIButton,UIImageView等设置圆角,设置阴影,设置边框的方法
- ios UIView设置背景图片的方法
- ios UIView设置背景图片的方法
- IOS设置UIView、UIButton等控件的边框设置为圆角
- iOS 圆角 圆形头像 设置方法
- UIView中UIButton的常用方法
- iOS UIView(UIButton)设置圆角和边框
- CALayer--设置UIView圆角,裁剪子视图,快速圆形UIView
- iOS设置UIButton的字体
- php图表fusioncharts使用实例
- ffmpeg混音(将多个声音合成一个)命令
- Android studio 的一些使用,如adb无线调试,用户名直接输入等
- onClick与onClientClick区别
- CSS3选择器
- iOS设置圆形(圆角)UIView、UIButton的简单方法
- 重新指派usb转串口模块在linux系统中的设备调用名称
- hdu hdoj 1598 find the most comfortable road【最小生成树】
- Android 之ExpandableListView几个特殊的属性
- 大话数据结构 第一章 读书笔记
- Sublime Text 3103 Crack 破解 注册码
- 加载大图多图内存溢出
- Java单元测试Junit(四)使用Cactus测试Servlet
- Java开发常用工具