使用用户

时间:2015-12-23 17:38:00

标签: swift cocoa nsview

我正在尝试使用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
    }
}

1 个答案:

答案 0 :(得分:0)

您是否阅读过文档?搜索“绘图”会显示Cocoa Drawing Guide

正如其他人所说,绘画是在drawRect()内完成的。在您通过将needsDisplay设置为true / YES进行标记后,系统将在“正确的时间”调用它。