Completely remove NSBezierPath?

172 Views Asked by At

In my code, I have a function that draws lines on the screen using the following code:

  func drawLine (pointArray:[[[Double]]]) -> NSBezierPath{

    var path:NSBezierPath = NSBezierPath()
    let color = NSColor(red:0.0, green: 1.0, blue:0.0, alpha: 1.0)
    color.set()
    for i in 0...(pointArray.count - 1){
    path.move(to: NSPoint(x: pointArray[i][0][0], y: pointArray[i][0][1]))
    path.line(to: NSPoint(x: pointArray[i][1][0], y: pointArray[i][1][1]))
    path.lineWidth = 50.0
    path.stroke()
    }


}

However, when I try to reset my display by drawing a white rectangle using:

func clear(){

        NSColor.white.setFill()
        bounds.fill()

    }

the lines still remain. I tried to get around this by setting a global variable for the path and changing my function to actually return the path so that I would have access to it outside of the function:

var path:NSBezierPath = NSBezierPath()

func drawLine (pointArray:[[[Double]]]) -> NSBezierPath{

    let color = NSColor(red:0.0, green: 1.0, blue:0.0, alpha: 1.0)
    color.set()
    for i in 0...(pointArray.count - 1){
    path.move(to: NSPoint(x: pointArray[i][0][0], y: pointArray[i][0][1]))
    path.line(to: NSPoint(x: pointArray[i][1][0], y: pointArray[i][1][1]))
    path.lineWidth = 50.0
    path.stroke()

  }

  return path

}

but I can't find a way to redraw over the path to get rid of it.

1

There are 1 best solutions below

0
Jawad Ali On

Set your bezierPath which is path in your case to nil which clears the old bezier path and then call [self setNeedsDisplay]. or you can use removeAllPoints on path and call [self setNeedsDisplay]

Happy coding =)