为UIView任意角设置圆角
2015-08-05 14:51
477 查看
圆角在苹果的世界时随处可见的。为视图设置圆角在开发iOS当中是经常要做的事情。要为UIView对象设置圆角也是非常简单地事情。
这样一句代码就轻松为一个view的四个角设置了圆角。 当我们需要为view任意一个角设置圆角的时候,需要怎么办呢?有一个方法就是通过layer.mask,也就是遮罩。加上UIBezierPath的
运行如下:
转自:http://supermao.cn/wei-uiviewren-yi-jiao-she-zhi-yuan-jiao/
view.layer.cornerRadius = 10;
这样一句代码就轻松为一个view的四个角设置了圆角。 当我们需要为view任意一个角设置圆角的时候,需要怎么办呢?有一个方法就是通过layer.mask,也就是遮罩。加上UIBezierPath的
+ (UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii的这个方法就可以轻松添加圆角遮罩了。
UIRectCorner
UIRectCorner的定义如下:typedef NS_OPTIONS(NSUInteger, UIRectCorner) { UIRectCornerTopLeft = 1 << 0, UIRectCornerTopRight = 1 << 1, UIRectCornerBottomLeft = 1 << 2, UIRectCornerBottomRight = 1 << 3, UIRectCornerAllCorners = ~0UL };
例子
下面代码就是为一个view三个角设置圆角。- (void)layoutSubviews{ [super layoutSubviews]; CGFloat corner = 50; CAShapeLayer *shapeLayer = [CAShapeLayer layer]; shapeLayer.path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft cornerRadii:CGSizeMake(corner, corner)].CGPath; self.layer.mask = shapeLayer; }
运行如下:
转自:http://supermao.cn/wei-uiviewren-yi-jiao-she-zhi-yuan-jiao/
相关文章推荐
- POJ 2533 Longest Ordered Subsequence
- UITableView中关于cell里的按钮被点击时如何确定是哪一个cell
- Marquee Effect
- Intel Threading BuildingBlocks(Intel TBB)介绍
- UIView和layer的关系
- OS开发UI篇—xib的简单使用
- 获取UINavgationController的退出事件
- IOS第七天(6:UiTableView编辑模式, 拖动位置 ,滑动删除)
- 类QQ,微信,联系人列表搜索UISearchBar使用
- IOS第七天(5:UiTableView 汽车品牌,复杂模型分组展示,A-Z索要列表) (2015-08-05 14:03)
- IOS第七天(4:UiTableView 数据的显示优化重复实例和tableFooterView和tableHeaderView)
- queue
- UITableView选中时颜色
- 【UILabel和UIButton】
- duilib各种布局的作用,相对布局与绝对布局的的意义与用法
- UISlider小结
- uva 11538 Chess Queen
- 自定义segue
- 最近项目中忽略的线程同步,ui更新等问题
- 最近项目中忽略的线程同步,ui更新等问题