简体   繁体   中英

How Do i create UIView like in the image?

i want to add curve in my UIView as shown in image. How can i create such uiview?

在此处输入图片说明

You can use UIBezierPath and use addCurve method to create your view.

//1. Create this new Class 
class ComplexView: UIView {

    var path: UIBezierPath!

    override init(frame: CGRect) {
        super.init(frame: frame)

        self.alpha = 0.3
        complexShape()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func draw(_ rect: CGRect) {
        // Specify the fill color and apply it to the path.
        UIColor.blue.setFill()
        path.fill()

        // Specify a border (stroke) color.
        UIColor.magenta.setStroke()
        path.stroke()
    }


    func complexShape() {
        path = UIBezierPath()
        path.move(to: CGPoint(x: 0.0, y: 0.0))

        path.addCurve(to: CGPoint(x: 0, y: self.frame.size.height),
                      controlPoint1: CGPoint(x: 50.0, y: 25.0),
                      controlPoint2: CGPoint(x: 50.0, y: self.frame.size.height - 25.0))

        path.close()

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

        self.backgroundColor = UIColor.orange
        self.layer.mask = shapeLayer
    }
}

In your ViewController call this View and add this view to your main view.

//2. In you Viewcontoller add ComplexView
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        let width: CGFloat = 100.0
        let height: CGFloat = 500.0

        let complexView = ComplexView(frame: CGRect(x: 0,
                                              y: self.view.frame.size.height/2 - height/2,
                                              width: width,
                                              height: height))

        self.view.addSubview(complexView)
    }

You need to play around addCurve method to get your desired shape.

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