简体   繁体   中英

How to set Corner Radius to 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()

在此处输入图片说明

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

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: Instead of all corner set the corners you want

价值一千字的图像

IOS 14, swift 5 version

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM