0

I have a subclassed NSView drawing column graphs in a portion of another NSView object.

The drawRect() function below draws the columns based on passed parameters correctly.

However, when I adjust the parameters and update the view (through myView.needDisplay = true) the next version of the columns draw over the top of the previous versions without removing them.

How do I remove the existing columns to redraw the next set? I was expecting the

backgroundColor.set()
NSBezierPath.fillRect(bounds)

portion of the code to overwrite the columns on each call to drawRect() - from the custom 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)
        }
    }
}

The answer here -> Clear NSView programmatically in swift

doesn't work as I don't have an NSWindow object.

The two images below show excerpts from the storyboard and the runtime output showing the view hierarchy and that the graphs plot ok.

Other relevant items of code might be:

The call from the viewController to an instance of the custom NSView object (GraphView in the storyboard).

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

Two of the relevant functions from the 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)
}
Community
  • 1
  • 1
Wolfstar
  • 111
  • 10
  • Are you sure you don't have another view lurking about in your view hierarchy? (You can confirm that by clicking on the view debugger button and rotating/expanding it a bit.) – Rob Jul 04 '15 at 04:52
  • I have tried drawing a few `NSRect`s and by calling `drawRect` consecutively (with a delay); the context does clear every time. I can't reproduce your problem. Perhaps showing a few more lines of code would be helpful. – Unheilig Jul 04 '15 at 05:27
  • Rob and Unheilig - thanks for responding. I have added some further code details. in the body. Unfortunately I can not post the images. The storyboard looks ok with respect to view hierarchy. The call from the view controller is initiated on selection of an item from a tableView. – Wolfstar Jul 04 '15 at 06:20
  • Ok - I have now found the error, it was in the parameter setting with an incorrect multiplier - while it looked like the view was not updating it was infact just updating exactly the same data each time. Doh! – Wolfstar Jul 04 '15 at 06:39
  • Yeah, it had to be something like that. Glad you solved it! – Rob Jul 04 '15 at 13:37

0 Answers0