Online Book Reader

Home Category

Learn Objective-C on the Mac - Mark Dalrymple [129]

By Root 982 0
in Cocoa

By now you’ve learned lots of powerful Cocoa techniques for dealing with data, optimally arranging your application’s classes, and using the wide range of on-screen controls and other views that are included with Cocoa. Now it’s time for you to start learning how to use Cocoa to make your own view classes, gaining complete control over the display of text and graphics. Mac applications are commonly known for including “eye candy”—not just for drawing needless graphical flourishes, but also for creating a more immersive interactive experience for the user. The graphics technologies available to you as a Cocoa programmer can help you achieve some of the same kinds of effects with a surprisingly small amount of effort on your part.

Core Graphics gives you rich functionality for rendering paths, manipulating coordinate systems, and more. One major part of Core Graphics is a set of APIs known as Quartz. Quartz, in fact, makes up such a huge part of Core Graphics that the two terms are sometimes used interchangeably. Core Animation takes things even further, letting you animate your views in a remarkably simple way.

In this chapter, we’ll cover some of the basics of coordinate systems and drawing into an NSView instance, and show you how to display a view larger than the space available to it in the window by putting it in a scrolling view. We’ll also touch on how you can easily add basic printing support to your applications.

Fundamentals


In this section, we’ll go through some of the basic concepts used in Cocoa’s drawing APIs. If you’ve done any sort of graphics programming, some of what’s available for drawing in Cocoa will be pretty familiar. After all, most of the basic concepts of computer graphics were hammered out decades ago. Where Cocoa stands out is both in the quality of its rendering, including anti-aliasing for both text and graphics primitives, as well as the availability of high-level abstractions for more complex concepts.

The View Coordinate System


Cocoa’s drawing systems work with an x-y coordinate system where, by default, the origin point (0,0) is at the lower left, the x-axis points to the right (increasing values take you further to the right) and the y-axis points up (increasing values take you further up on the screen). This is similar to the way graphs are traditionally done in mathematics, but flipped from many other computer graphics systems, where the y-axis points down (increasing values take you further down on the screen). Although it’s possible and sometimes desirable to flip the y-axis in Cocoa, we won’t be doing so in our examples. Also, by default, one unit along either axis corresponds to one screen pixel.

Drawing is normally done by instances of NSView subclasses. Views exist in a hierarchy. If the top-most view in the hierarchy is set to be the contentView of an NSWindow, then all the views in that hierarchy are displayed within that window. The top-level view in a window, the contentView, can draw across the entire area of the window. For every other view in the hierarchy, the drawing area is limited to its frame, a space within the coordinates of its parent view (see Figure 13-1).

Figure 13-1. A simple hierarchy of views. The window’s contentView contains an NSBox, which in turn contains an NSForm.

Frame Rectangle vs. Bounds Rectangle


For each view in a window, there are a couple of important rectangles (or “rects” as they are commonly called) that define a lot about its drawing characteristics: its frame rect and its bounds rect. The frame rect simply specifies the view’s location and size within its parent view’s coordinate space. For example, imagine the top-level view in a window. Let’s call it A. If A has a subview called B which extends from point (10,20) to point (40,60), then B’s frame rect has (10,20) as its origin and (30,40) as its size.

Apart from its frame, which determines where and to what extent it appears in its superview, each view has a bounds rect, which defines its own inner coordinate space.

For example, view B described

Return Main Page Previous Page Next Page

®Online Book Reader