0
我試圖用Swift繪製圖形。我創建了一個自定義NSView
和一個NSView
類。 在ViewController
我想調用一個方法來更新NSView
並使用用戶輸入的值繪製完整的圖形。使用用戶輸入的值在NSView中繪製圖形
import Cocoa
class Graph: NSView {
let startPoint = NSPoint(x: 10, y: 10)
override func drawRect(dirtyRect: NSRect) {
NSColor.whiteColor().set() // set white color
NSRectFill(dirtyRect) // fill the Rect in the View
// draw axes lines
NSColor.blackColor().set()
NSBezierPath.strokeLineFromPoint(startPoint, toPoint: NSPoint(x: Double(dirtyRect.width) - 10.0, y: 10.0))
NSBezierPath.strokeLineFromPoint(startPoint, toPoint: NSPoint(x: 10.0, y: Double(dirtyRect.height) - 10.0))
}
func drawGraphicsOfNewteork(arrayOfData: [NSTextField]){
// Here I would draw in the View some lines using the arrayOfData
}
}
從'drawRect'內調用它。 –
執行'drawRect'內的所有繪圖。如果要更新圖形,請調用'setNeedsDisplayInRect'。 – Willeke