Online Book Reader

Home Category

Learn Objective-C on the Mac - Mark Dalrymple [145]

By Root 937 0
code for the two internal methods referenced here, prepareRightSide and transitionInFromRight. We won’t bother putting these methods into a separate protocol or anything. In Objective-C, code can freely call any other methods declared higher up in the same @implementation block, even if those methods aren’t declared in any @interface, so all we have to do is put these internal methods somewhere above applicationDidFinishLaunching:, so that the code in there can call these methods. The first of these, prepareRightSide, starts off by determining the index of the next view to display, by adding one to currentTabIndex and then doing a simple bounds check on the new index, resetting it to zero if it’s gotten too high. Then we use that index to grab the next view, and set its frame to be the same size as box, shifted off to the right by exactly the width of box so that it’s just out of sight. We set its alpha value to 0.0, making it effectively invisible, and finally we add the view as a subview of box, so that it will actually be displayed.

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

Return Main Page Previous Page Next Page

®Online Book Reader