3

I am trying to draw a line on an UIView with an UIBezierpath. I think I am missing something, but wasn't able to find it out.

Here is my code:

// Code for touch recognition
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    swiped = false
    if let touch = touches.first as? UITouch? {
        lastPoint = (touch?.location(in: fullview))!
        //print(lastPoint)
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    swiped = true
    if let touch = touches.first as? UITouch? {
        let currentPoint = touch?.location(in: fullview)
        drawLineFrom(fromPoint: lastPoint, toPoint: currentPoint!)

        lastPoint = currentPoint!
        //print(lastPoint)
        //print("touch moved")
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if !swiped {
        drawLineFrom(fromPoint: lastPoint, toPoint: lastPoint)
    }
    //print("touch ended")
}

//code for drawing
func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint){

    UIGraphicsBeginImageContext(fullview.frame.size)

    let context = UIGraphicsGetCurrentContext()

    let aPath = UIBezierPath()

    //aPath.move(to: fromPoint)
    //aPath.addLine(to: toPoint)
    aPath.lineWidth=10.0
    aPath.lineJoinStyle = .round
    aPath.move(to: CGPoint(x:15,y:15))
    aPath.addLine(to: CGPoint(x:80,y:80))
    aPath.addClip()
    aPath.close()
    UIColor.green.set()
    aPath.stroke()

    //print("drawline")
    //print("Frompoint = ",fromPoint)
    //print("topoint = ",toPoint)

  /*  context?.setLineCap(.round)
    context?.setLineWidth(brushWidth)
    context?.setStrokeColor(red: red, green: green, blue: blue, alpha: 1.0)
    context?.setBlendMode(.normal)

    context?.beginPath()
    context?.move(to: fromPoint)
    context?.addLine(to: toPoint)
    context?.closePath()
    context?.strokePath()*/


    //let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    fullview.setNeedsDisplay()
}

As you can see, I tried it also with the context, but it wasn't working too.

user513951
  • 12,445
  • 7
  • 65
  • 82
Tristan G
  • 1,720
  • 2
  • 13
  • 22

2 Answers2

11

I use this to draw a line:

    let doYourPath = UIBezierPath(rect: CGRect(x: xPos, y: yPos, width: yourWidth, height: yourHeight))
    let layer = CAShapeLayer()
    layer.path = doYourPath.cgPath
    layer.strokeColor = UIColor.white.cgColor
    layer.fillColor = UIColor.white.cgColor

    self.view.layer.addSublayer(layer)

Hope this help you out. This is just one the way to draw a line in swift.

Mar-k
  • 648
  • 6
  • 16
3

Well, you are drawing into an image context (an offscreen bitmap), not into the view. That is quite likely not what you want? Make sure you have read iOS Drawing Concepts.

Your UIView subclass should probably just track the positions of the touches (like in a var touchedPositions = [CGPoint]()) and call setNeedsDisplay(). Then implement the func draw(_ rect: CGRect) method in your subclass. Within this method, create your path and draw it according to the positions you tracked (w/o creating a new context).

hnh
  • 13,957
  • 6
  • 30
  • 40