我在另一个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)
}
}
}
答案在这里 - > Clear NSView programmatically in swift
不起作用,因为我没有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)
}