Game Development Reference
In-Depth Information
The reason we want our layout logic in a separate task is that we also want to call this task when the
view controller first loads, so the UI is correctly set up when the game starts. This lets us put all of
our orientation logic in one place.
The first time, the state of these objects will be different. For example, the first time this task is
called, neither the portraitView nor the landscapeView is attached to the scene, so the call to
removeFromSubview will be a nonoperation, without error. When this task is called on subsequent
rotations, rockPaperScissorsController's view will be attached to the scene; however, when a view
is added as a subview, it is automatically removed from its parent view, if it has one.
The fact that removeFromSuperview and addSubview: have these reasonable default behaviors makes
life pretty easy when it comes to manipulating views. It cuts down on error-prone cleanup code.
Listing 2-8 shows how we call setOrientation: when the application first starts.
Listing 2-8. ViewController.m (viewDidLoad)
- (void)viewDidLoad
{
[super viewDidLoad];
UIDevice* device = [UIDevice currentDevice];
[self setOrientation: [device orientation]];
}
In Listing 2-8, the task viewDidLoad is called as the ViewController is done initializing and is
displayed on our screen. Here, we simply get the current device's orientation and pass that value to
setOrientation: , ensuring the UI is oriented correctly when the application starts.
If you run the project, you will see the app running on both the iPad and the iPhone, changing its
layout based on orientation.
Summary
In this chapter, we looked at how to create an Xcode project suitable for developing a universal
application. We explored the initialization process of an iOS application, and saw where to start
modifying the project to create the application we want. We looked at how XIB files and classes
interact, and at how iOS exploits MVC with the UIViewController class. We created an application
that seamlessly supports running on an iPhone or an iPad in all orientations, setting the stage for
game development.
 
Search WWH ::




Custom Search