0

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.

Jawad Ali
  • 13,556
  • 3
  • 32
  • 49
Ztan
  • 25
  • 1
  • 6
  • isn't it possible to just remove the layer to which you applied that path? – SWAT Feb 29 '20 at 07:04
  • @SWAT - Ztan is not adding layers, but rather stroking paths. – Rob Feb 29 '20 at 07:13
  • Ztan, from where are you calling these methods? Perhaps you can provide a complete [MCVE](https://stackoverflow.com/help/mcve) rather than snippets, like this. Bottom line, usually you call them from `draw(_:)`, and when you want them re-rendered, you call `setNeedsDisplay`, which triggers `draw(_:)` to be called (in which you stroke what needs stroking and don’t stroke what shouldn’t). But you don’t generally “redraw over the path.” Or, as SWAT suggested, when you want to add a path to be rendered, you add a `CAShapeLayer` and when you want the path to be removed, you remove that sublayer. – Rob Feb 29 '20 at 07:18
  • I'm not sure I can provide any useful MCVE. I was hoping there was a way to simply "unstroke" a path. – Ztan Feb 29 '20 at 07:30
  • Unstroking just isn’t a concept that makes any sense or that is relevant. When you re-render a view, you just stroke what’s to be rendered, and anything you don’t stroke isn’t rendered. So it’s not “stroke” and “unstroke” but rather just either “stroke it” or “don’t”. The only time where the concept of “unstroking” would make any sense is if you stroked something and rendered it into a bitmap, at which point it’s baked in and you just can’t unstroke, but rather generally have to reconstruct it from scratch. But you’re not showing us how you’re calling `drawLine`, so we can’t help you. – Rob Mar 04 '20 at 08:31
  • E.g. if you want to re-render a `NSView`, you just set its [`needsDisplay`](https://developer.apple.com/documentation/appkit/nsview/1483360-needsdisplay), at which point your [`draw(_:)`](https://developer.apple.com/documentation/appkit/nsview/1483686-draw) is called, and anything you stroke is rendered and anything you don’t, won’t be. – Rob Mar 04 '20 at 08:33

1 Answers1

0

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 =)

Jawad Ali
  • 13,556
  • 3
  • 32
  • 49