Learn Objective-C on the Mac - Mark Dalrymple [145]
The next method, transitionInFromRight, takes rightView and slides it into place so that it fits perfectly into the space provided by box. It also sets the alpha value to 1.0, making it fully opaque. Note that unlike the previous method, this uses rightView’s animator method to access the view’s animation proxy, so that setting these values actually creates implicit animations for us.
Before we go any further, let’s check our work by building and running our app. This won’t quite work as-is, because we declared action methods in our header file but haven’t defined them yet. Add the following methods to the .m file:
Now, you should be able to Build & Run your app, and see the first item from your tab view slide into place and fade in from invisibility to full opacity at the same time, as shown in Figure 14-8.
Figure 14-8. The first “page” is sliding into view. Note the slightly grayed-out appearance of the objects in the box, which are all at about 50 percent opacity at this point.
That’s a start! Now let’s see how we move on to the next item in the list, by providing an implementation for the next: method. Some of this code is similar to what we had in the applicationDidFinishLaunching: method. We prepare the right side, start some transitions (including a call to another new internal method, transitionOutToLeft, which we’ll get to soon), and update an index (including another bounds check) and some pointers at the end. The biggest difference here is that the methods that are going to do animation are all sandwiched between calls to [NSAnimationContext beginGrouping] and [NSAnimationContext endGrouping], which work together to form a sort of transaction. Between those two calls, any animations that are added to the default animation context, including all implicit animations, will be set up to run simultaneously. This means that as we create implicit animations in our internal methods, they will all be set up to fire off simultaneously. Without this step, the animations we create would all start running in sequence, one after another as they’re created. Normally this won’t make much of a difference, but it’s entirely possible that some unexpected event could occur just as those animations are being created, such as another process suddenly hogging the CPU, which could lead to these animations running in a slightly staggered manner, starting and ending at different times. By wrapping them in a grouping as shown here, that potential problem is eliminated.
The next: method also calls another internal method, transitionOutToLeft, which will take the current view and shuffle it off to the left. Its implementation looks like this:
With that in place, you’re now ready to Build & Run once again. This time you’ll see that not only does the initial view setup work, but now you can also hit the Next button to transition to the next view! Smooth. See Figure 14-9.
Figure 14-9. View Three is on its way out, View Four is almost halfway in.
Now all that’s left