如何将Corner Radius设置为UIBezierPath [英] How to set Corner Radius to UIBezierPath

查看:130
本文介绍了如何将Corner Radius设置为UIBezierPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码 UIBezierPath 创建了以下图片

I have created following image by UIBezierPath using this code

    //// Bezier Drawing
    let bezierPath = UIBezierPath()
    bezierPath.move(to: CGPoint(x: 360, y: 0))
    bezierPath.addCurve(to: CGPoint(x: 360, y: 75), controlPoint1: CGPoint(x: 359.53, y: 30.5), controlPoint2: CGPoint(x: 359.91, y: 55.45))
    bezierPath.addCurve(to: CGPoint(x: 360, y: 153.78), controlPoint1: CGPoint(x: 360.35, y: 153.21), controlPoint2: CGPoint(x: 360, y: 153.78))
    bezierPath.addCurve(to: CGPoint(x: 180, y: 153.78), controlPoint1: CGPoint(x: 360, y: 153.78), controlPoint2: CGPoint(x: 271.2, y: 212.77))
    bezierPath.addCurve(to: CGPoint(x: 0, y: 153.78), controlPoint1: CGPoint(x: 88.8, y: 94.8), controlPoint2: CGPoint(x: 0, y: 153.78))
    bezierPath.addLine(to: CGPoint(x: 0, y: 0))
    bezierPath.addLine(to: CGPoint(x: 360, y: 0))
    UIColor.green.setFill()
    bezierPath.fill()

现在我想给角落半径 TopLeft TopRight 以下图片使用Slider。

now i want to give corner radius to TopLeft and TopRight of this following Image using Slider.

我试过以下代码,但它对我不起作用。

I have Tried Following code but it did not works for me.

        var cornerPath = path

        cornerPath = UIBezierPath(roundedRect:self.imgTop.bounds,
                                          byRoundingCorners:[.topLeft, .topRight],
                                          cornerRadii: CGSize(width: CGFloat(cornerRadius),
                                                              height: CGFloat(cornerRadius)))

        path.lineWidth = 1.0

        let maskLayer = CAShapeLayer()
        maskLayer.path = cornerPath.cgPath
        self.imgTop.layer.mask = maskLayer







    let maskLayerTop = CAShapeLayer()
    maskLayerTop.fillColor = UIColor.white.cgColor
    maskLayerTop.backgroundColor = UIColor.clear.cgColor
    maskLayerTop.path = pathTop?.cgPath
    maskLayerTop.cornerRadius = CGFloat(cornerRadius)
    //maskLayerTop.masksToBounds = true

    maskLayerTop.shadowRadius = CGFloat(cornerRadius)
    maskLayerTop.fillColor = UIColor.green.cgColor
    maskLayerTop.shadowColor = UIColor.blue.cgColor

    imgTop.layer.mask = maskLayerTop

我还尝试将角半径应用于图像视图,但它不起作用。我想要角落半径,如下图所示。

I had also tried to applying corner radius to Image view but it did not work.i want corner radius like following image.

请帮忙!

Please Help! .

注意:我已创建一个创建以下图片的路径

NOTE: I had already create one path to create following Image

推荐答案

在你的bezierPath = UIBezierPath()之前添加曲线

before your let bezierPath = UIBezierPath() for curve add

var = cornerRadiudSize = CGSize(width: 50.0, height: 50.0) //this is your global variable that you can change based on the slider and then draw
let p = UIBezierPath(roundedRect: rect, byRoundingCorners: .allCorners, cornerRadii: cornerRadiudSize)
p.addClip()

PS:而不是所有角落设置你想要的角落

PS: Instead of all corner set the corners you want

这篇关于如何将Corner Radius设置为UIBezierPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆