为控件指定角设置圆角大小

我们在实际项目开发中可能会遇到对控件的某个角设置圆角,并且可能不同角的圆角大小也不同。在这里记录一下我的解决办法:

    let testView = UIView(frame: CGRect(x: 50, y: 100, width: 200, height: 100))
    testView.backgroundColor = UIColor.red
    //在这里统一设置四个角的圆角
    testView.layer.cornerRadius = 5
    testView.layer.masksToBounds = true
    self.view.addSubview(testView)
    
    //通过贝塞尔曲线画圆角
    let fieldPath:UIBezierPath = UIBezierPath.init(roundedRect: testView.bounds,           
    //我在这里指定了三个角,设置不同的圆角大小
    byRoundingCorners: [.topLeft,.bottomRight,.bottomLeft] , cornerRadii:     
    CGSize(width: 15, height: 15))
    let fieldLayer:CAShapeLayer = CAShapeLayer()
    fieldLayer.frame = testView.bounds
    fieldLayer.path = fieldPath.cgPath
    testView.layer.mask = fieldLayer

效果图如下:

image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容