【发布时间】:2015-12-23 17:38:00
【问题描述】:
我正在尝试使用 Swift 绘制图表。我创建了一个自定义 NSView 和一个 NSView 类。
在ViewController 中,我想调用一个方法来更新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。