Rob
Rob

Reputation: 164

How to draw a curve like this in UIBezierPath Swift?

I am trying to develop a screen whose background looks like this:

enter image description here

Here I am trying to develop the gray curved background and it fills the lower part of the screen as well. I'm very new to UIBezierPath and I've tried this:

class CurvedView: UIView {

//MARK:- Data Types

//MARK:- View Setup

override func draw(_ rect: CGRect) {
    
    let fillColor: UIColor = .blue
    let path = UIBezierPath()
    let y:CGFloat = 0
    
    print(rect.height)
    print(rect.width)
    
    path.move(to: CGPoint(x: .zero, y: 100))
    path.addLine(to: CGPoint(x: 60, y: 100))
    path.addCurve(to: .init(x: 100, y: 0), controlPoint1: .init(x: 125, y: 80), controlPoint2: .init(x: 50, y: 80))
    
    path.close()
    fillColor.setFill()
    path.fill()
    
}

override init(frame: CGRect) {
    super.init(frame: frame)
    self.backgroundColor = .init(hex: "#dfe1e3")
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.backgroundColor = .init(hex: "#dfe1e3")
    
}
}

This code gave me this:

enter image description here

I followed a lot of tutorials but I didn't get the exact understanding. I understood that for this curve I have to move to (0,100) and then add a line and then add a curve and ten extend the line add a curve then straight line lower curve and then straight line and close. But, when I started as you can see the blue line didn't cover the upper part. Can any one please help me?

Upvotes: 0

Views: 2903

Answers (2)

Nithin.P Molethu
Nithin.P Molethu

Reputation: 109

enter image description herein reference to @aiwiguna

class CurvedView: UIView {

//MARK:- Data Types

//MARK:- View Setup

override func draw(_ rect: CGRect) {
    
  
    let path = UIBezierPath()
  
    
    let fillColor = UIColor.blue
            
            let y: CGFloat = UIScreen.main.bounds.size.height
            let x: CGFloat = UIScreen.main.bounds.size.width
            let height: CGFloat = 200
            
            path.move(to: CGPoint(x: 0, y: y)) // bottom left
            path.addLine(to: CGPoint(x: 0, y: y - 20)) // top left
            path.addCurve(to: CGPoint(x: x, y: y - height), controlPoint1: CGPoint(x: x * 2 / 3, y: y), controlPoint2: CGPoint(x: x * 5 / 6, y: y - height * 6 / 5)) // curve to top right
            path.addLine(to: CGPoint(x: x, y: y)) // bottom right
            path.close() // close the path from bottom right to bottom left

            let shapeLayer = CAShapeLayer()
        shapeLayer.path = path.cgPath
        shapeLayer.fillColor = fillColor.cgColor

    
    path.close()
    fillColor.setFill()
    path.fill()
    
}

override init(frame: CGRect) {
    super.init(frame: frame)
    self.backgroundColor = .red
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.backgroundColor = .yellow
    
}
}

Upvotes: 0

aiwiguna
aiwiguna

Reputation: 2922

Here some example that I create, you can change the value to make it more similar to what you want

Here a guide how control point in a curve work

Note: I called this code in viewDidload

let path = UIBezierPath()
        let fillColor = UIColor.blue
        
        let y: CGFloat = UIScreen.main.bounds.size.height
        let x: CGFloat = UIScreen.main.bounds.size.width
        let height: CGFloat = 200
        
        path.move(to: CGPoint(x: 0, y: y)) // bottom left
        path.addLine(to: CGPoint(x: 0, y: y - 20)) // top left
        path.addCurve(to: CGPoint(x: x, y: y - height), controlPoint1: CGPoint(x: x * 2 / 3, y: y), controlPoint2: CGPoint(x: x * 5 / 6, y: y - height * 6 / 5)) // curve to top right
        path.addLine(to: CGPoint(x: x, y: y)) // bottom right
        path.close() // close the path from bottom right to bottom left

        let shapeLayer = CAShapeLayer()
    shapeLayer.path = path.cgPath
    shapeLayer.fillColor = fillColor.cgColor

    view.layer.addSublayer(shapeLayer)

enter image description here

Upvotes: 2

Related Questions