0

我在另一个 NSView 对象的一部分中有一个子类 NSView 绘制柱形图。

下面的 drawRect() 函数根据传递的参数正确绘制列。

但是,当我调整参数并更新视图(通过 myView.needDisplay = true)时,下一个版本的列会在之前版本的顶部绘制而不删除它们。

如何删除现有列以重绘下一组?我期待着

backgroundColor.set()
NSBezierPath.fillRect(bounds)

用于覆盖每次调用 drawRect() 的列的代码的一部分 - 从自定义 NSView。

override func drawRect(dirtyRect: NSRect) {
    backgroundColor.set()
    NSBezierPath.fillRect(bounds)
    println("Drawn")

    if checkBoxCurrentMonth == true {
        for object in columnDetails{
            drawChartColumn(bounds.size, parameters: object)
        }
    }
}

答案在这里->以编程方式快速清除 NSView

不起作用,因为我没有 NSWindow 对象。

下面的两张图片显示了故事板的摘录和显示视图层次结构的运行时输出,并且图表绘制正常。

其他相关的代码项可能是:

从 viewController 到自定义 NSView 对象(故事板中的 GraphView)实例的调用。

        // Call the view methods to draw the new columns
        graphView.confirmCurrentMonth()
        graphView.determineColumnColor(currentMonthColor)
        graphView.determineColumnParameters(columnDetailsArray)
        graphView.needsDisplay = true

GraphView() 中的两个相关函数:

func drawChartColumn(size: CGSize, parameters: (columnHeight: CGFloat, columnTotalNumber: CGFloat, thisColumnNumber: CGFloat)) {

    let (colHeight, columnFrame) = determineColumnSize(size, columnTotalNumber: parameters.columnTotalNumber, columnHeight: parameters.columnHeight, thisColumn: parameters.thisColumnNumber)
    columnColor.set()
    NSBezierPath(rect: columnFrame).fill()
}

func determineColumnSize(size: CGSize, columnTotalNumber: CGFloat, columnHeight: CGFloat, thisColumn: CGFloat) ->(columnHeight: CGFloat, columnFrame: CGRect) {

    // determine total size of the graphview
    let graphHeight = size.height / 2
    let graphWidth = size.width - graphOrigin.x - rightMargin
    let columnAndGapSize = graphWidth / 25
    let columnX: CGFloat = (graphOrigin.x + columnAndGapSize) + (columnAndGapSize * thisColumn) * 2
    let columnY: CGFloat = (graphOrigin.y) + columnHeight * (graphHeight - topMargin - bottomMargin)

    // determine the drawing bounds and the column frame assuming that the full bounds are used (ie no padding)
    let drawingBounds = CGRectMake(columnX, graphOrigin.y, columnAndGapSize, columnY)//= CGRect(x: columnX , y: graphOrigin.y, width: columnAndGapSize , height: columnY)
    let columnFrame = drawingBounds

    println("\(columnHeight) is the columnHeight. \(columnFrame) is the columnFrame.")

    return (columnHeight, columnFrame)
}
4

0 回答 0