Draw a line with UIBezierPath

前端 未结 4 1904
礼貌的吻别
礼貌的吻别 2020-12-08 04:22

First time using BezierPaths, wondering how this function is actually supposed to be implemented. Currently the bezier path moves within the frame of the image, as opposed t

4条回答
  •  时光取名叫无心
    2020-12-08 04:44

    Swift 3.1 Version of William Falcon's Answer + Improved

    This is just an updated version of already accepted answer, and I just added a little bit more to it.

    func drawLineFromPointToPoint(startX: Int, toEndingX endX: Int, startingY startY: Int, toEndingY endY: Int, ofColor lineColor: UIColor, widthOfLine lineWidth: CGFloat, inView view: UIView) {
    
        let path = UIBezierPath()
        path.move(to: CGPoint(x: startX, y: startY))
        path.addLine(to: CGPoint(x: endX, y: endY))
    
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = path.cgPath
        shapeLayer.strokeColor = lineColor.cgColor
        shapeLayer.lineWidth = lineWidth
    
        view.layer.addSublayer(shapeLayer)
    
    }
    

    And of course the implementation would be

    drawLineFromPointToPoint(startX: Int, toEndingX: Int, startingY: Int, toEndingY: Int, ofColor: UIColor, widthOfLine: CGFloat, inView: UIView)
    

    What I changed from accepted answer

    I changed the vars to lets, and made it easier to input the start and end of both the x and the y. I also allow the user to change the width of the line.

    I chose for the values to be of type Int, but you can change those to be the other allowed options.

提交回复
热议问题