Learn Objective-C on the Mac - Mark Dalrymple [137]
Now save your work, and Build & Run your app. In addition to what you implemented earlier, you can now print from the menu, just like nearly every other Mac OS X application. Invoking the print command brings up the standard Mac OS X print panel, letting you choose which printer, open in Preview, and so on. You get all of this for free, just for programming in Cocoa!
Wrapping Up
In this chapter, you’ve gained a lot of understanding of the workings of NSView and some other classes that deal with drawing. However, there’s still lots more you can do with Cocoa’s drawing facilities, including drawing more interesting curves, modifying your drawing in response to mouse events, and animating your views, all of which we’ll cover in the next chapter.
Chapter14
Advanced Drawing Topics
In Chapter 13, you gained some basic knowledge of Cocoa’s key drawing concepts, such as using paths to describe shapes, copying images to the screen, and rendering text. In this chapter, we’re going to expand upon this, getting you comfortable with a few techniques that will bring your graphics to life. In the first section, we’ll show you how to make a view respond to mouse events, letting users interact with your customized views. In the second section, we’ll give you a brief introduction to Core Animation, an exciting technology that lets you create smooth animations with just a few lines of code.
Editing a Curve
In chapter 13, we introduced you to the NSBezierPath class for drawing rounded rectangles, ovals, straight lines, and points. If you’ve used a Bezier drawing tool in Photoshop or other applications, you may have wondered what those shapes have to do with Bezier curves at all! A Bezier curve is essentially a series of points describing a path, and control points describing the curves between the points. As such, basically any shape that can be drawn with a pen (in the real world, or virtually in a computer graphics system) can be described as a Bezier curve, including straight lines and jagged angles. However, as a layman’s term, Bezier curve usually means something more along the lines of what you see here in Figure 14-1.
Here, the black curve is a Bezier curve, defined by two endpoints (the lower-left and upper-right corners) and two control points, depicted here by rather gigantic circles at the end of sticks. By dragging the control points around, the user can change the shape of the curve. A view like this can be useful as a pacing control, determining the rate of change of some value over time, such as the movement of object from one point to another. Make the curve into a straight line to specify a perfectly linear transition, or make a sort of S-shape to make a value start changing slowly, quickly ramp up halfway through, and then slow down as it approaches the target value (sometimes known as an “ease-in/ease-out” transition). This control is what we’re going to be implementing in this section.
Figure 14-1. A Bezier curve
Preparations
Create a new Cocoa project in Xcode and name it CurveEdit. Do the usual steps for turning on garbage collection. If you’re running on Snow Leopard or later, the new project will contain a class called CurveEdit_AppDelegate. If you’re running on Leopard, you’ll need to create the class, and add it to the MainMenu.xib file as usual.
The interesting part of this application will all be in the view object, but first let’s take care of the infrastructure surrounding the view. We’re going to stick with the MVC architecture for this project, which will help make sure that the view we create will be usable as a standalone component. The text