Macos 从NSView对象中清除图形
我有一个子类NSView,它在另一个NSView对象的一部分中绘制列图 下面的drawRect()函数根据传递的参数正确绘制列 但是,当我调整参数并更新视图(通过myView.needDisplay=true)时,列的下一个版本将在先前版本的顶部绘制,而不会删除它们 如何删除现有列以重新绘制下一组?我在等那场比赛Macos 从NSView对象中清除图形,macos,swift,cocoa,nsview,nsbezierpath,Macos,Swift,Cocoa,Nsview,Nsbezierpath,我有一个子类NSView,它在另一个NSView对象的一部分中绘制列图 下面的drawRect()函数根据传递的参数正确绘制列 但是,当我调整参数并更新视图(通过myView.needDisplay=true)时,列的下一个版本将在先前版本的顶部绘制,而不会删除它们 如何删除现有列以重新绘制下一组?我在等那场比赛 backgroundColor.set() NSBezierPath.fillRect(bounds) 用于覆盖自定义NSView中每次调用drawRect()时的列的代码部分 ov
backgroundColor.set()
NSBezierPath.fillRect(bounds)
用于覆盖自定义NSView中每次调用drawRect()时的列的代码部分
override func drawRect(dirtyRect: NSRect) {
backgroundColor.set()
NSBezierPath.fillRect(bounds)
println("Drawn")
if checkBoxCurrentMonth == true {
for object in columnDetails{
drawChartColumn(bounds.size, parameters: object)
}
}
}
答案在这里->
不起作用,因为我没有NSWindow对象
下面的两幅图像显示了故事板的摘录和运行时输出,其中显示了视图层次结构,并显示了图的绘制状态
其他相关的代码项可能是:
从viewController调用自定义NSView对象的实例(故事板中的GraphView)
GraphView()中的两个相关函数:
你确定你的视图层次结构中没有隐藏其他视图吗?(您可以通过单击view debugger按钮并稍微旋转/展开它来确认。)我尝试了绘制一些
NSRect
s,并连续调用drawRect
(延迟);上下文每次都很清楚。我无法重现你的问题。也许多展示几行代码会有帮助。Rob和Unheilig-感谢您的回复。我添加了一些进一步的代码细节。在身体里。不幸的是,我不能张贴的图像。相对于视图层次结构,情节提要看起来正常。视图控制器的调用是在从tableView中选择一个项目时启动的。好的-我现在发现了错误,它是在参数设置中使用了不正确的乘数-虽然看起来视图没有更新,但实际上每次都在更新完全相同的数据。啊!是的,一定是这样的。很高兴你解决了!
// Call the view methods to draw the new columns
graphView.confirmCurrentMonth()
graphView.determineColumnColor(currentMonthColor)
graphView.determineColumnParameters(columnDetailsArray)
graphView.needsDisplay = true
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)
}