有时候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,更厉害的是,切去的部分不响应用户点击!

这种方法,只要稍作修改就能实现很多效果,比如一个多边形的按钮,把一张图片切成邮票。

最新文章

  1. Javascript与ECMAScript
  2. 2014年7月份第2周51Aspx源码发布详情
  3. [NOIP2012] 提高组 洛谷P1081 开车旅行
  4. iOS - KVC 键值编码
  5. mysql备份脚本
  6. poj 3628 Bookshelf 2
  7. Arcgis for Silverlight学习(一)
  8. 字符串---分割成数组(str_split ),算出一个字符串中出现最多的字符, 学校中最多的姓名
  9. CCNA网络工程师学习进程(10)NAT的配置
  10. python爬取快手视频 多线程下载
  11. MessengerJS
  12. vue 构建项目 文件引入
  13. Python数据分析Numpy库方法简介(四)
  14. shell脚本学习-变量
  15. 仿B站项目(3)页面配置
  16. Golang的panic和recover
  17. Java编程思想 学习笔记3
  18. Consul实践指导-DNS接口
  19. SetProcessWorkingSetSize 降低程序运行内存
  20. 实现接口时@Override注解问题

热门文章

  1. LabVIEW设计模式系列——资源关闭后错误处理
  2. IOS 整理
  3. hdu 1199 Color the Ball(离散化线段树)
  4. UIWindow详解
  5. Java实现微信菜单json字符串拼接
  6. (转)ie浏览器判断
  7. js - 在拼接字符串中动态submit当前form
  8. 分享red hat linux 6上安装oracle11g时遇到的gcc: error trying to exec 'cc1': execvp: No such file or directory的问题处理过程
  9. C语言之分配
  10. 【转】iOS开发常用的第三方类库