【问题标题】:Draw a graph in a NSView using values entered by the user使用用户输入的值在 NSView 中绘制图形
【发布时间】: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

标签: swift cocoa nsview


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 2016-08-23
    • 2022-01-24
    • 1970-01-01
    相关资源
    最近更新 更多