I am developing a Mac app in Swift which does graphics animations in two windows. One is a Metal window (MTKView) which works well. The other one plots a graph as a subclass of NSView and is connected to the Metal window using an outlet (which works). When I want to update the second window, I use "graph.needsDisplay = true", where graph is the name of the outlet.
For some reason, the draw function does not execute when I use setNeedsDisplay in the MTKView class, whose draw command executes 60 times a second. I have verified that the draw function does work, since the graph is plotted when I resize the window, as it is supposed to do.
I have a version of this program (written using Obj. C and OpenGL ES) for iOS which I wrote about 5 years ago and it works fine. The draw function is the exact equivalent of the code below (in Obj. C):
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() } }
Can anyone help? I have been trying everything under the sun and nothing works. I have been doing development of the Mac and iOS for more than 20 years, but, to be honest, have very little deep understanding of object oriented programming.
I submitted a query on a similar question which was answered about 2 days ago. Thanks in advance.