How to set Corner Radius to UIBezierPath
Asked Answered
T

2

4

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

enter image description here

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.

enter image description here

Please Help! .

.

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

Troyes answered 26/5, 2018 at 9:20 Comment(2)
did you set view to clip to bound to true? self.imgTop.clipsToBounds = trueSolangesolano
@IraniyaNaynesh It does not workSpaghetti
S
3

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

image worth a thousand words

Solangesolano answered 26/5, 2018 at 10:39 Comment(11)
Hey, Thanks for your effort. Can you please send me your Project?Troyes
there is nothing in the project just one file that i have already postedSolangesolano
I have used your code and it works perfect with view but i want to do this with Image, I tried to add image but it did not work. Can you please help me?Troyes
I don't know you can add path to image but can do it to imageViewSolangesolano
why can't you just do yourImageView.layer.cornerRadius = 30 ? you don't need to create path for thisSolangesolano
It won't work because i am creating photo editing app so that i am using path for Curve layer and i had tried about image.layer.cornerRadius but it will remove curve from my ImageTroyes
Let us continue this discussion in chat.Solangesolano
@IraniyaNaynesh Please request accept for this demo code.Costive
@Digvijay What? didn't get itSolangesolano
@IraniyaNaynesh I need your drive demo zip file. I'm enable to download demo from that link.Costive
@Digvijay Sorry but I haven't shared any drive link, also as I said earlier there is only one imp file and code that is already attached above. If you are stuck with something, tell me that If I can I will surely help. Thanks.Solangesolano
A
0

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
Adalbertoadalheid answered 24/12, 2020 at 8:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.