Learn Objective-C on the Mac - Mark Dalrymple [143]
Now, every time the user clicks the Move button, the resulting animation’s timing function will be determined by the values in the CurveView control. Save your work, Build & Run your app, and you should see this happening. Drag the handles around to create different curve shapes, click the Move button, and see how it moves.
Grouping Animations
You’ve now gotten a taste of how Core Animation works, but in the somewhat silly context of randomly moving a button around the screen. Not really a GUI design that we’d recommend! In the real world, Core Animation is most often used to animate transitions between different views. Chances are, you’ve seen this used again and again on the iPhone (the platform that Core Animation was really built for, before being “backported” to Mac OS X). All the smooth slides, scales, and fades that occur throughout the iPhone interface are implemented with Core Animation. In Mac OS X, Core Animation isn’t quite so omnipresent, but it’s put to good use in places like the “coverflow” view mode, which first appeared in iTunes and is now a part of Finder as well. In this section, you’ll see how to implement some nice transitions yourself, by grouping animations together so that they run simultaneously.
In Xcode, by create a new Cocoa project called FlipIt. As usual, turn on garbage collection, and make sure you have a FlipIt_AppDelegate class both in the Xcode project, and properly connected in MainMenu.xib. What we’re going to do is present a GUI where the user can flip between several “pages,” and Core Animation will animate nicely between them. We’ll use a box in the nib’s empty window to show the content pages, which themselves will be held in an NSTabView. We won’t display the tab view itself, we’re just using it as a handy container for our content pages.
Start by defining the interface for our controller class. It contains outlets for the two views we need to manage: tabView, the object containing the views we’re going to display, and box, the on-screen view where we’ll be displaying them. It also has instance variables for pointing at the views that are actively transitioning in and out of focus, as well as an array for holding all the available views, and an integer index to identify the current focused view. Finally, our interface declares a pair of action methods that will be used by a matching pair of buttons in the GUI to tell our controller to flip between views.
Now open MainMenu.xib in Interface Builder, where we’ll define our views. Start off by dragging a button from the Library to the bottom of your GUI’s empty window. Then duplicate the button, and title the two buttons Previous and Next. Connect the buttons to the matching action methods in the app delegate, and place the two buttons side by side at the lower center of the window (see Figure 14-5).
Figure 14-5. Preparing the window
Now find an NSBox in the Library and drag it to the empty window, placing it above the buttons and resizing it to fill most of the screen. Use the Attributes Inspector to remove the box’s title by setting the Title Pos popup to None (see Figure 14-6). Then connect the app delegate’s box outlet to the NSBox, so that we can reach it from code.
Figure 14-6. The display window is now ready.
Our next course of action is to set up a set of views for switching in and out of the main view. We’ll use an NSTabView for this, simply because it’s a convenient way for us to build a series of views in Interface Builder that can later be maintained as a list of offscreen views when our application runs. Find an NSTabView in the Library, but drag it to the main nib window instead of the GUI window you’ve been building. You’ll see that the tab view appears as a top-level icon in the nib window, alongside the app delegate, the window, and other items (see Figure 14-7).
Figure 14-7. It’s an unusual place to find a tab view, but there it is.
A tab view (or any other NSView subclass) placed at the top level of a nib file won’t be displayed when the nib is loaded, but