0
已经找了很多东西,但没有找到匹配。 真实简单我想画一条线购买一个按钮。 我有一个来自NSView的类来完成绘图,它监视一个var,以及它是否改变重画视图。 我可以通过改变数值来获得鼠标点击来调用视图,它画得很好。但我不能让按钮工作。它调用绘图方法,但视图不更新? 我知道它很简单,但我找不到它。 在此先感谢。OSX Swift,按一个按钮画一条简单的线
下面是我的View类和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
}
}
谢谢Paulo,它让我走上了正轨。我还没有告诉过你我有自定义的视图,所以使用的大小等。 – HelpWoody
@HelpWoody好吧,很棒:)祝你的程序好运! –