在UIView的swift 3中的bezierPath,只需要曲线

时间:2017-09-28 16:02:20

标签: ios swift uikit

当我在UIView上使用swift绘制bezierPath时。我和它一起直线。我只需要曲线就可以去掉直线。

我可以删除灰色填充: line.fillColor = UIColor.clear.cgColor

不确定我如何删除直线

enter image description here

代码:

    let line = CAShapeLayer()
    let linePath = UIBezierPath()
    linePath.move(to: start)
    linePath.addLine(to: end)
    var dis = (end.x - start.x)/3.0
    linePath.addCurve(to: start,
                  controlPoint1: CGPoint(x: start.x + dis, y: start.y + dis),
                  controlPoint2: CGPoint(x: end.x - dis, y: end.y - dis))

    line.path = linePath.cgPath
    line.strokeColor = UIColor.red.cgColor
    //line.fillColor = UIColor.clear.cgColor
    line.lineWidth = 4.0
    line.opacity = 0.6
    self.view.layer.addSublayer(line)

2 个答案:

答案 0 :(得分:3)

不要关闭路径。当您关闭贝塞尔曲线路径时,它会在路径的开头和结尾之间添加一条直线。如果你不关闭路径,那么它不应该这样做。 (但是你不能填充非封闭的路径,只需要抚摸它。)

如果这对您没有帮助,您需要编辑问题以显示创建和绘制路径的代码。

答案 1 :(得分:2)

请尝试以下代码。指定起点和终点,并使用三角函数的pi表示圆曲线。如果您想使用这些动画,请将以下代码添加到UIView

enter image description here     导入UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.



        let pi = CGFloat(Float.pi)
        let start:CGFloat = 0.0
        let end :CGFloat = pi

        // circlecurve
        let path: UIBezierPath = UIBezierPath();
        path.addArc(
            withCenter: CGPoint(x:self.view.frame.width/4, y:self.view.frame.height/4),
            radius: 300,
            startAngle: start,
            endAngle: end,
            clockwise: true
        )
        let layer = CAShapeLayer()
        layer.fillColor = UIColor.clear.cgColor
        layer.strokeColor = UIColor.orange.cgColor
        layer.path = path.cgPath
        self.view.layer.addSublayer(layer)


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}