Ios 如何将角半径设置为UIBezierPath

Ios 如何将角半径设置为UIBezierPath,ios,swift,uibezierpath,Ios,Swift,Uibezierpath,我通过使用此代码创建了以下图像 //// 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.9

我通过使用此代码创建了以下图像

    //// 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()

现在我想使用滑块为下图的
左上
右上
指定角半径

我尝试了以下代码,但它对我不起作用

        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

我也尝试过将角半径应用到图像视图中,但没有成功。我希望角半径如下图所示

请帮忙!


注意:在让bezierPath=UIBezierPath()用于曲线添加之前,我已经创建了一个路径来创建以下图像

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:设置所需的角点,而不是所有角点


IOS 14,swift 5版本

let bezierPath = UIBezierPath(roundedRect: shareContainerView.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 4, height: 4))
let maskLayer = CAShapeLayer()
maskLayer.path = bezierPath.cgPath
shareContainerView.layer.mask = maskLayer

是否已将“视图”“剪辑”“绑定”设置为“真”?self.imgTop.clipstobunds=true@IraniyaNaynesh没用了嘿,谢谢你的努力。你能给我发送你的项目吗?项目中没有任何内容,只有一个文件,我已经发布了。我已经使用了你的代码,它在视图中运行得很好,但是我想用图像来执行此操作,我尝试添加图像,但它不起作用。你能帮帮我吗?我不知道你能为图像添加路径,但能为ImageView添加路径为什么不能只添加ImageView.layer.cornerRadius=30?您不需要为此创建路径
let bezierPath = UIBezierPath(roundedRect: shareContainerView.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 4, height: 4))
let maskLayer = CAShapeLayer()
maskLayer.path = bezierPath.cgPath
shareContainerView.layer.mask = maskLayer