Learn Objective-C on the Mac - Mark Dalrymple [144]
Double-click the tab view icon, and you’ll see a window appear containing just the tab view. This window is just something Interface Builder uses to give you a structure to edit the tab view in, and does not represent an actual NSWindow in your nib file. You can verify this by clicking in the window’s title bar and observing the title bar of the Inspector window, which always tells you what it’s inspecting. In this case, selecting the tab view’s window title bar will still show “Tab View” in the Inspector title bar, unlike normal windows you edit which will change it to “Window.” One side effect of this is that the tab view displayed in this way doesn’t have the resize controls on all four sides and all four corners like it would in a normal window. Instead, you can use the resizing gadget at the window’s lower-right corner to resize your view. Do that now, making it about the same size as the NSBox you put into the window a little while ago.
Now let’s put some content into this tab view, the “pages” that we’ll be able to flip between. By default the tab view contains just a few content views, but feel free to increase that number (using the Attributes Inspector) so that you have even more views to switch between. The actual content isn’t that important, as long as something is unique on each page so that you can easily see the content change from one page to the next. A good start is just to grab a label from the Library, give it a nice big font, and change its text to the word “One.” Then copy this label and paste it into each of the other views (which you can switch between using the tabs at the top, just like otherwise), changing the label accordingly each time. Just for fun, add some unique items to each page as well (a table view here, a set of buttons there) so that when the app is done, you’ll see a little more things in motion while flipping between pages. Save your work, and go back to Xcode.
It’s time to start implementing our app delegate class in FlipIt_AppDelegate.m. This class will have a number of methods for preparing a transition by setting up the next view to be displayed, out to the side of the box; transitioning the new view into position; and transitioning the current view out to the other side. Because we want to be able to do transitions in two directions, depending on whether we’re flipping to the right or the left, each of those methods exists in two forms, setting things up and executing a rightward flip or leftward flip. In addition, we’ll implement our two action methods that start up the transitions, and a startup method (applicationDidFinishLaunching:) that sets up the initial view.
Let’s start by creating a preprocessor definition, ANIM_DURATION, to define the duration (in seconds) of the animations we’re going to create. By putting this in one place, at the top of the file, we can easily experiment, tweaking this setting until we find a speed we like. Define it like this:
Now let’s move on to the applicationDidFinishLaunching: method. Here we grab the list of views from the tabView, and set up currentTabIndex to point at the end of the array so that the first item will be lined up (more on this later). Then we call the first of our internal methods, prepareRightSide, which will set up the next view for display on the right side of the box. Then we make use of the ANIM_DURATION value, using it to specify the duration of any animations we create within the current animation context. Then we call another internal method, transitionInFromRight, which will start up the animations to move the next view into the right position. Finally we set currentTabIndex to point at item zero (the first object in the items array).
Now let’s write the