我们在项目开发过程中都会遇到 产品狗提出的一些设计,UI美工给的一些奇葩设计图。。。。
可是我们是程序员,还是忍了。。。。
下面关于iOS中控件切除一个或者多个圆角。
其实切一个圆角你会了,切多个圆角当然可以类似啦。。。。文章后面会讲到一个枚举。
此处给列举一个切图片圆角的例子。。。。
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 4.5, 49.5)];
imgView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:imgView];
UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:self.view.bounds byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(5, 5)];
CAShapeLayer* shape = [[CAShapeLayer alloc] init];
[shape setPath:rounded.CGPath];
imgView.layer.mask = shape;
这样就可以切除图片的一个圆角了。。。。。
要想做到切除控件的多个圆角,往下看。。。
只需要在UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:self.view.bounds byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(5, 5)]; 中的byRoundingCorners,可以使用到有以下四个枚举数值。。。。
UIRectCornerTopLeft 左上
UIRectCornerTopRight 右上
UIRectCornerBottomLeft 左下
UIRectCornerBottomRight 右下
此处告一段落。
------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
下面说一下,在tableViewCell中出现以下这样的

左侧颜色条,我使用的是UIImageView,右侧深蓝色宽条使用的是UIView。。。。
针对UIImageView,我使用的就是上述讲的方法,枚举设置左上和左下。
而下面重头戏来了!!!针对UIView的右侧大圆角,你会怎么做了?看到这点,你肯定会想还用上边那个方法。。。(估计也可以,等下我给尝试下,给个结果)
先讲个简单的方法,就是在controller中的cellForRow,可以直接使用一个私有的方法来设置,addRoundedCorners:UIRectCornerTopRight|UIRectCornerBottomRight withRadii:CGSizeMake(cell.frame.size.height, cell.frame.size.height)];
其实这种方法也很类似于上侧讲述的方法,但是这种写个自定义的UIView的扩展类,也挺不错的。。。。
这种私有扩展类,仅供参考。。。。。
如果有更好的方法,欢迎指点,谢谢!🙏
网友评论