Learn Objective-C on the Mac - Mark Dalrymple [140]
That’s it! Build & Run, and you’ll see that whenever you’re dragging the first control point, it appears in front of the second.
It would also be nice to highlight the control point that’s currently being dragged, maybe by drawing it with a different color. This is also a pretty easy change that gives the user some useful feedback. Start by defining some highlight colors for a new gradient, adding these lines among the other #defines at the top of the file:
Now, modify the drawControlPointAtX:y: methods, adding an additional parameter to specify whether or not do draw the highlighted variant, and the bold lines shown here:
Because we added a parameter to the control-point drawing method, we need to also change the way it’s called at the end of drawRect, like this:
Now Build & Run, and you’ll see that the otherwise gray control points now light up red while dragging, giving the user a nice visual cue.
Core Animation: A Primer
One of the most exciting technologies Apple includes with Mac OS X is a graphics system called Core Animation, which lets you easily create animated effects in your applications. You can make your views slide, fade, rotate, and scale smoothly and easily, often with just a few lines of code. In essence, Core Animation lets you specify a change in an object—such as changing its location to a different spot in the window—in such a way that, instead of the change happening instantaneously, it’s automatically split up into several small movements that are rendered over time by Core Animation. You can specify a transition’s length in seconds, as well as the timing or pacing of the change. You can also group animations together, so that they all execute in perfect synchrony.
Core Animation Basics
From a technical standpoint, the basic unit at the heart of all this is a class called CALayer (a pre-release version of Core Animation was even called Layer Kit). Each NSView can optionally have a CALayer attached to it, either by flicking a switch in Interface Builder or setting it up in code. The process of assigning a layer to a view actually begins a recursive process through all the view’s subviews, so that when a view has a layer, all its subviews (and all their subviews, and so on) also acquire layers. Once a layer is in place, you can start animating the view.
Under the hood, each CALayer is associated with some OpenGL structures for rendering its graphics. OpenGL does a really great job of quickly drawing rectangles to the screen, even rectangles that are resized, rotated, and the like, so using CALayer lets you have views that do all sorts of on-screen tricks without slowing your application down. The Core Animation APIs shield you completely from OpenGL itself, so it will work away quietly without you having to think about it too much. The only thing to bear in mind is that each layer uses some amount of the memory available to the computer’s graphics hardware, so you’re better off using layers only for those parts of your application where you actually want to do some animation, instead of applying them to every view in every window.
Implicit Animations
Any layer-backed view can be animated by using its animator proxy. This is a special object that acts as a substitute for the view itself, setting up an animation corresponding to the method it’s sent instead of making an immediate change. For instance, if you want to animate the movement of a view, then instead of setting its frame like this:
you can set it like this:
To see this in action, create a new Cocoa project,