ios view 切上部分圆角_ios中给view添加圆角并指定位置

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;

比较推荐使用第三种,内存消耗少,速度快。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值