美文网首页
iOS UIView设置圆角 指定区域圆角

iOS UIView设置圆角 指定区域圆角

作者: 前行的骆驼 | 来源:发表于2020-03-18 10:15 被阅读0次

iOS的UIView提供了基本的设置圆角的方法,即:

        view.layer.cornerRadius= cornerRadius;

但是在项目中常常遇到需要在指定位置设置圆角,当然我们常常用UI切图的方式来实现,但是我们用代码也可以实现这一效果。

首先,iOS 11以上系统也提供了设置方法,即在设置cornerRadius的基础上,再设置圆角的方向maskedCorners:

        view.layer.maskedCorners = (CACornerMask)(UIRectCornerTopLeft|UIRectCornerTopRight);

而在iOS 11.0以前我们就需要用贝塞尔曲线来绘制圆角了,即:

        UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];

        CAShapeLayer*maskLayer = [[CAShapeLayeralloc]init];

        maskLayer.frame=view.bounds;

        maskLayer.path= path.CGPath;

        view.layer.mask= maskLayer;

相关文章

网友评论

      本文标题:iOS UIView设置圆角 指定区域圆角

      本文链接:https://www.haomeiwen.com/subject/trqqyhtx.html