ios中给view添加圆角并指定位置
在ios开发中,为了有个不错的ui交互效果,我们经常会用到为视图添加圆角,或者指定某个位置去切割圆角。
* 简单实现*具体有三种方式:
第一种:设置图层的属性
使用简单,性能不好 在开发中我们应该减少使用
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];
2、第二种使用贝塞尔曲线uibezierpath,开启图形上下文画出一个圆
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();
//结束画图--这个时候的image是圆形的
uigraphicsendimagecontext();//结束
[self.view addsubview:imageview];
3、第三种使用uibezierpath和casharelayer设置圆角
uiview *view1 = [[uiview alloc]initwithframe:cgrectmake(10, 100, 300, 400)];
view1.backgroundcolor = [uicolor redcolor];
[self.view addsubview:view1];
uibezierpath *path = [uibezierpath bezierpathwithroundedrect:view1.bounds byroundingcorners:uirectcornertopright| uirectcornertopleft cornerradii:cgsizemake(10, 10)];//指定圆角位置 大小
cashapelayer *masklayer = [[cashapelayer alloc]init];
masklayer.frame = view1.bounds;
masklayer.path = path.cgpath;
view1.layer.mask = masklayer;
比较推荐使用第三种,内存消耗少,速度快。
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!