macosswift3drawingnsviewdrawing2d

OSX Swift , Draw a simple line with a push of a button


Have looked a lot for this but nothing found to match. Real simple I want to draw a line buy a push of a button. I have a class which is from NSView to do the drawing, this monitors a var and if it changes to redraw the view. I can get the mouse click to call the view by changing the value and it draws just fine. But I cant get the button to work. It calls the drawing method but the view is not updated ??? I know its got to be something simple, but I just cannot find it. Thanks in advance.

Below is my View class and View Controller.

    //
//  LineDrawer.swift
//  TestDraw3
//
//  Created by Colin Wood on 21/04/17.
//  Copyright © 2017 Colin Wood. All rights reserved.
//

import Cocoa



class LineDrawer : NSView {
// Vars
    var newLinear = NSBezierPath()


    public var QQ: Int = 0 {   // every time QQ changes redraw information in QQ
        didSet{
            boom()    // drawing method
        }
    }



// Methods
    override func draw(_ dirtyRect: NSRect) {
        NSColor.red.set()
        newLinear.lineWidth = 5
        newLinear.lineCapStyle = NSLineCapStyle.roundLineCapStyle
        newLinear.stroke()
    }

    func boom(){  // draw a simple line
        Swift.print("In BooM")
        Swift.print(QQ)
        let lastPt = NSPoint(x: QQ, y: 1)
        newLinear.move(to: lastPt)
        let newPt = NSPoint(x: QQ, y: 50)
        newLinear.line(to: newPt)
        needsDisplay = true
    }

     override func mouseDown(with theEvent: NSEvent) {
        QQ = QQ + 7  // just add 7 so you can see it being drawn after didSet 
    }

}


//
//  ViewController.swift
//  TestDraw3
//
//  Created by Colin Wood on 15/04/17.
//  Copyright © 2017 Colin Wood. All rights reserved.
//

import Cocoa

class ViewController: NSViewController {
// Vars
    @IBOutlet weak var textField: NSTextField!

// Methods
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.

        }
    }


    @IBAction func button(_ sender: Any) {
        let v = LineDrawer()
        v.QQ = 300  // change QQ to call didSet
    }

}

Solution

  • You need to add your LineDrawer as a subview. A good place, to add it, is in the viewDidLoad method:

    private var line: LinerDrawer!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        line = LinerDrawer()
        line.frame = NSMakeRect(0, 0, 200, 200)
        line.QQ = 300
        line.isHidden = true
        view.addSubview(line)
    }
    

    And then finally show it when you hit the button:

    @IBAction func button(_ sender: Any) {
        line.isHidden = false
    }
    

    You still need to work out the correct frame for your LineDrawer view.