Drawing a line on NSView in swift
Asked Answered
A

2

6

That's my code at the moment:

class LineDrawer : NSView {
required init?(coder  aDecoder : NSCoder) {
    super.init(coder: aDecoder)
}

var line : Array<Line> = []
var lastPt : CGPoint!

override func mouseDown(theEvent: NSEvent) {
    super.mouseDown(theEvent)
    let location = theEvent.locationInWindow
    println(location)

}
override func mouseDragged(theEvent: NSEvent) {
    super.mouseDragged(theEvent)
    var newPt = theEvent.locationInWindow
    line.append(Line(start: newPt, end: lastPt))
    lastPt = newPt
}
override func drawRect(dirtyRect: NSRect) {

}
}

class Line {
var start : CGPoint
var end : CGPoint
init(start _start : CGPoint, end _end : CGPoint) {
    start = _start
    end = _end
}
}

And I just don't have any ideas how to draw the line with a selected color (e.g. black) for each line in line array. I'm new to swift, so I'll be grateful for the comprehensive explanation.

Accepter answered 30/1, 2015 at 13:31 Comment(0)
V
8

Like this:

class SomeView:NSView {

  override func drawRect(dirtyRect: NSRect) {
    NSColor.redColor().set() // choose color
    let figure = NSBezierPath() // container for line(s)
    figure.moveToPoint(NSMakePoint(x, y)) // start point
    figure.lineToPoint(NSMakePoint(x+10.0, y+10.0)) // destination
    figure.lineWidth = 1  // hair line
    figure.stroke()  // draw line(s) in color
  }
}

I guess this is mostly self explaining. The coordinates are that which you use inside the view's frame.

If the lines are not updating then you need

view.needsDisplay = true

in your viewController. Put in a println to see that the view is actually re-drawn.

Villanovan answered 30/1, 2015 at 13:46 Comment(11)
BTW, the lines appear only when I switch to another app and then return, is there any function to update view automatically?Accepter
view.needsDisplay = trueVillanovan
Thanks, but where to write that? I tried in mouseDown like self.needsDisplay = true, but the drawing still loads after hiding and bringing back the windowAccepter
Oh. I see you have a NSView. Put the code in override drawRectVillanovan
Doesn't work, lines are still updating only after hiding and bringing back the windowAccepter
May be, the view is re-drawn, but the lines don't appear anyway. I haven't found anyone who knows what's actually happens there yetAccepter
Probably you should open another question with your new codeVillanovan
I have, there's itAccepter
Hehe. You did not put it inside the drawRect as I told you.Villanovan
Yes, I didn't, because it don't work too. But I'll try again, if you want :)Accepter
See my answer over there.Villanovan
S
4

For swift 5+ and MacOS:

override func draw(_ dirtyRect: NSRect) {
    NSColor.red.set()
    let figure = NSBezierPath()
    figure.move(to: NSMakePoint(100, 100)) // {x,y} start point
    figure.line(to: NSMakePoint(110.0, 120.0)) //  {x,y} destination
    figure.lineWidth = 1  // hair line
    figure.stroke()  // draw line
}
Salinometer answered 6/6, 2019 at 14:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.