教你把UIView切成任意形状
2024-08-25 19:47:07
有时候layer.cornerRadius并不能满足需求,自己实现drawRect又太麻烦,怎么办?
多的不说,直接上代码:
- (void)dwMakeBottomRoundCornerWithRadius:(CGFloat)radius
{
CGSize size = self.frame.size;
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
[shapeLayer setFillColor:[[UIColor whiteColor] CGColor]]; CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, size.width - radius, size.height);
CGPathAddArc(path, NULL, size.width-radius, size.height-radius, radius, M_PI/2, 0.0, YES);
CGPathAddLineToPoint(path, NULL, size.width, 0.0);
CGPathAddLineToPoint(path, NULL, 0.0, 0.0);
CGPathAddLineToPoint(path, NULL, 0.0, size.height - radius);
CGPathAddArc(path, NULL, radius, size.height - radius, radius, M_PI, M_PI/2, YES);
CGPathCloseSubpath(path);
[shapeLayer setPath:path];
CFRelease(path);
self.layer.mask = shapeLayer;//layer的mask,顾名思义,是种位掩蔽,在shapeLayer的填充区域中,alpha值不为零的部分,self会被绘制;alpha值为零的部分,self不会被绘制,甚至不会响应touch
} - (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self dwMakeBottomRoundCornerWithRadius:3.0];
}
}
这样就能切出一个只有下半部圆角的View,更厉害的是,切去的部分不响应用户点击!
这种方法,只要稍作修改就能实现很多效果,比如一个多边形的按钮,把一张图片切成邮票。
最新文章
- Javascript与ECMAScript
- 2014年7月份第2周51Aspx源码发布详情
- [NOIP2012] 提高组 洛谷P1081 开车旅行
- iOS - KVC	键值编码
- mysql备份脚本
- poj 3628 Bookshelf 2
- Arcgis for Silverlight学习(一)
- 字符串---分割成数组(str_split ),算出一个字符串中出现最多的字符, 学校中最多的姓名
- CCNA网络工程师学习进程(10)NAT的配置
- python爬取快手视频 多线程下载
- MessengerJS
- vue 构建项目 文件引入
- Python数据分析Numpy库方法简介(四)
- shell脚本学习-变量
- 仿B站项目(3)页面配置
- Golang的panic和recover
- Java编程思想 学习笔记3
- Consul实践指导-DNS接口
- SetProcessWorkingSetSize 降低程序运行内存
- 实现接口时@Override注解问题
热门文章
- LabVIEW设计模式系列——资源关闭后错误处理
- IOS 整理
- hdu 1199 Color the Ball(离散化线段树)
- UIWindow详解
- Java实现微信菜单json字符串拼接
- (转)ie浏览器判断
- js - 在拼接字符串中动态submit当前form
- 分享red hat linux 6上安装oracle11g时遇到的gcc: error trying to exec 'cc1': execvp: No such file or directory的问题处理过程
- C语言之分配
- 【转】iOS开发常用的第三方类库