Adding Mouse Tracking to GraphPaper
Now we know enough to add mouse tracking to GraphPaper. The modifications will involve the following four parts:
Modifying the initWithFrame: method to set up the tracking rectangle around the on-screen ColorGraphView instance
Adding a mouseEntered: method that will tell the NSWindow to start sending mouse-moved events
Adding a mouseMoved: method to process mouse-moved events
Adding a mouseExited: method that will reset the window’s event-handling status
Rather than adding this new functionality to the GraphView or ColorGraphView classes, we’ll subclass ColorGraphView to make a TrackingGraphView class. Tracking functionality is separate from graphing functionality, and it makes sense to separate them in the code.
Let’s get on with it!
Changes to the GraphPaper Interface
Back in IB, make sure that
MainMenu.nib
is open and is the selected window (again, we recommend that you minimize the other nibs that are open in IB).Subclass the ColorGraphView class and rename the new subclass “TrackingGraphView”.
Add outlets called
xCell
andyCell
to the TrackingGraphView class.Change the class of the ColorGraphView instance in the GraphPaper window to TrackingGraphView in the Class Info dialog.
Verify that the Graph button is still connected to the TrackingGraphView instance. Make sure it sends the graph: action.
Add two NSTextField objects inside the GraphPaper window and label them “x:” and “y:”, as shown in Figure 18-1. It may be necessary to resize the ...
Get Building Cocoa Applications: A Step by Step Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.