BarView: An NSView with a Scaled Coordinate System

In this section we’ll subclass NSView to create a class called BarView to demonstrate a scaled coordinate system. A BarView object will display a simple bar graph that draws a graph between the range to 100, depending on the value of a slider. It will scale its coordinate system and control redrawing with the appropriate display methods. We’ll use the same ViewDemo project we created for BlackView so that we won’t have to go through the project-creation process again.

  1. Back in IB, remove all BlackView instances from your ViewDemo window (select them one by one and type Control-X).

  2. Select the NSView class under the Classes tab of IB’s Nib File window and subclass NSView by choosing IB’s Classes Subclass NSView menu command.

  3. Change the name of the new subclass from “MyView” to “BarView”.

  4. Drag a CustomView icon from IB’s Cocoa-Containers palette and drop it in the empty window.

  5. Type Command-1, then change the class of the CustomView to BarView in the NSView (Custom) Info panel.

  6. Resize the BarView instance, as shown in Figure 15-5.

  7. Drag a vertical slider from IB’s Cocoa-Other palette and drop it next to the BarView, as shown in Figure 15-5.

  8. With the slider selected, type Command-1 and note that the slider has a default range (Minimum/Maximum) from 0.0 to 100.0 and a Current setting of 50.0.

  9. Change the Current value of the slider to 0.0 in the NSSlider Info panel.

  10. Make sure that the Marker Values Only checkbox is not checked so that ...

Get Building Cocoa Applications: A Step by Step Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.