Thanks very much for the response. After some experimentation, I discovered that needsDisplay quickly gets reset.
My problem is that the draw method doesn't respond to needsDisplay. If I resize the view, it redraws while I am doing that, but I guess that is what draw is expected to do. I just want it to respond to needsDisplay.
My drawing class looks like this:
| |
| class GraphView: NSView { |
| |
| |
| override func draw(_ dirtyRect:NSRect) |
| { |
| super.draw(dirtyRect) |
| var width:Float = 0.0, height:Float |
| let backgroundColor = NSColor.cyan |
| var iGraph, iData, i1:Int |
| var x,y:Int |
| backgroundColor.set() |
| NSBezierPath.fill(bounds) |
| height=Float(dirtyRect.size.height) |
| width=Float(dirtyRect.size.width) |
| let path=NSBezierPath() |
| NSColor.black.setStroke() |
| path.lineWidth = 0.5 |
| path.move(to:NSPoint(x:0, y:Int(height)/2)) |
| path.line(to:NSPoint(x:Int(width), y:Int(height)/2)) |
| path.stroke() |
| current+=1 |
| if current>200 { |
| current = 0 } |
| points[current]=jz |
| i1=current |
| x = 0 |
| y = Int(0.25*height*(1.0-points[i1]/height_vector)) |
| path.move(to:NSPoint(x:0, y:Int(y))) |
| for i in 0..<200 |
| { |
| x = i*Int(width/200.0) |
| if i1<=0 {i1+=200} |
| y = Int(0.5*height - 0.45*height*points[i1]/height_vector) |
| path.line(to:NSPoint(x:x,y:y)) |
| i1 -= 1 |
| } |
| path.stroke() |
| } |
| } |
The needsDisplay is issued in another class (an animated MTKView) and needsDisplay=true is placed so that it is called at each iteration of the inner loop of the Metal view (60 fps). Everything compiles fine, but I can't get the above draw function to execute.
This app has an interesting history. I first wrote in about 20 years ago on the Mac using Obj. C and OpenGL. About 5 years ago I ported it to iOS using the OpenGL ES, a much more difficult version of OpenGL. I have now ported it back to the Mac writing it in Swift and using Metal. The Metal code is a modification of code I found on the web and works very well, displaying some 3D vectors rotating around a magnetic field vector (to illustrate nuclear magnetic resonance). The iOS code was my template and doesn't seem to have this problem (though it is written in a UIViewController).
I hope this sheds some light on my problem and sorry for the length of this post. I must confess that I never had a deep understanding of object oriented programming, even in the old days of OS X (I also developed on a NeXT). Much of it is trial and error with a little help from Hillegass' book on Cocoa programming.