已经为此寻找了很多,但没有找到匹配的。真的很简单我想画一条线买一个按钮。我有一个来自 NSView 的类来进行绘图,它监视一个 var,如果它改变以重绘视图。我可以通过更改值来单击鼠标来调用视图,并且绘制得很好。但我无法让按钮工作。它调用绘图方法但视图没有更新???我知道它必须是一些简单的东西,但我就是找不到它。提前致谢。
下面是我的视图类和视图控制器。
//
// 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
}
}