iOS开发之指定UIView的某几个角为圆角
我们知道, 如果需要将UIView的4个角全部都为圆角,做法相当简单,只需设置其Layer的cornerRadius属性即可(项目需要使用QuartzCore框架)。而若要指定某几个角(小于4)为圆角而别的不变时,这种方法就不好用了。
对于这种情况,Stackoverflow上提供了几种解决方案 。其中最简单优雅的方案,就是使用UIBezierPath。下面给出一段示例代码:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(120,10,80,80)];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10,10)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = view.bounds;
maskLayer.path = maskPath.CGPath;
view.layer.mask = maskLayer;
其中,
byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight
指定了需要成为圆角的角。该参数是UIRectCorner类型的,可选的值有:
* UIRectCornerTopLeft
* UIRectCornerTopRight
* UIRectCornerBottomLeft
* UIRectCornerBottomRight
* UIRectCornerAllCorners
从名字很容易看出来代表的意思,使用“|”来组合就好了。
最新文章
- 【转】js实现复制到剪贴板功能,兼容所有浏览器
- 新特性AAtitti css3 新特性attilax总结titti css
- Cache-Aside Pattern解析
- 通过设置虚拟机(ubantu15.10)的分辨率达到全屏效果
- hdu3033 分组背包
- Android调用系统关机与重启功能
- 怎样改动Myeclipse10.7的Servlet模板
- R0~R16寄存器作用(转)
- CentOS7 安装sentry(最新)
- cnblog 模板 SimpleMemory 个性化设置代码备份
- zabbix监控实战<;3>; 之自定义监控实例
- A2D JS框架 - Web API CSRF保护实现
- 爬虫(三)之scrapy核心组件
- maven项目出现红色感叹号报错
- 日期时间函数 mysql 和sqlserver 中对于常用函数的日期和时间函数的区别
- agc007D - Shik and Game(dp 单调性)
- windows下 jemalloc编译
- pkg_config找不到库
- 在初学Flask中遇到的小问题。
- Easy File Sharing Web Server 6.9远程溢出漏洞