【问题标题】:How to draw a curve like this in UIBezierPath Swift?如何在 UIBezierPath Swift 中绘制这样的曲线?
【发布时间】:2020-07-17 07:40:28
【问题描述】:

我正在尝试开发一个背景如下所示的屏幕:

在这里,我正在尝试开发灰色弯曲背景,它也填充了屏幕的下部。我对UIBezierPath 很陌生,我试过这个:

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")
    
}
}

这段代码给了我这个:

我遵循了很多教程,但我没有得到确切的理解。我知道对于这条曲线,我必须移动到(0,100),然后添加一条线,然后添加一条曲线,然后十延长线添加一条曲线,然后直线下曲线,然后直线并关闭。但是,当我开始时,你可以看到蓝线没有覆盖上部。谁能帮帮我?

【问题讨论】:

    标签: ios swift core-graphics uibezierpath


    【解决方案1】:

    这是我创建的一些示例,您可以更改值以使其与您想要的更相似

    这里是guide 曲线中的控制点是如何工作的

    注意:我在 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)
    

    【讨论】:

    • 您好,感谢您的回答。我需要在 `draw(_ rect: ) 函数的哪里添加它并保持其他东西完全相同?
    • 你需要改变你的绘图功能
    • 您的问题中已经有了函数,只需更改函数内部的代码并将 x,y,height 数字更改为您想要的值
    • 是的,我已将您的代码添加到draw 函数中,但它不起作用。
    • 也更改 x,y,height,因为它现在绘制在屏幕底部,您的视图可能没有那个大小
    【解决方案2】:

    引用@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
        
    }
    }
    

    【讨论】:

    • 是的,我已经这样做了。他在viewDidLoad中使用过。
    猜你喜欢
    • 2012-06-09
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    • 2014-07-19
    • 1970-01-01
    • 2016-02-04
    相关资源
    最近更新 更多