Online Book Reader

Home Category

iOS Recipes - Matt Drance [4]

By Root 218 0
do this, but let’s start with a very simple approach that should be usable from just about anywhere. We’ll start by tackling an iPhone app in portrait orientation and then move on to an iPad variant that supports all orientations. You can see the initial screens in Figure 1, Splash screen vs. initial UI .

Figure 1. Splash screen vs. initial UI

* * *

The simplest possible “splash screen transition” is a fade between the default image and the UI. It’s cheap and easy and can make a world of difference for the user experience. Think about it: this is the very first thing your users see. There’s no reason for this introduction to be anything but smooth.

To fade the default image offscreen, we need to first show a view that displays the same image and then fade that view out. This is pretty easy to do: we’re going to build a simple view controller that’s usable from just about any project. This view controller takes a custom splash image and defines a ‑hide method that executes the fade.

BasicSplashScreen/PRPSplashScreen.h

@interface PRPSplashScreen : UIViewController {}

@property (nonatomic, retain) UIImage *splashImage;

@property (nonatomic, assign) BOOL showsStatusBarOnDismissal;

@property (nonatomic, assign) IBOutlet id delegate;

- (void)hide;

@end

The interface also has a delegate property, declared as an id . That PRPSplashScreenDelegate protocol is defined in a separate header for communicating the splash screen’s status to an interested party: when the screen appears, when the transition begins, and when it ends.

You’ve surely acted as a delegate in plenty of places, but you may not have defined one before. Take a look at the protocol declaration and note the @optional keyword, which means the delegate does not have to implement all of the declared methods. An object that wants to know the splash screen’s state can now declare itself as conforming to PRPSplashScreenDelegate, implement one or more of the delegate methods, and assign itself to the splash screen’s delegate property.

BasicSplashScreen/PRPSplashScreenDelegate.h

@protocol PRPSplashScreenDelegate

@optional

- (void)splashScreenDidAppear:(PRPSplashScreen *)splashScreen;

- (void)splashScreenWillDisappear:(PRPSplashScreen *)splashScreen;

- (void)splashScreenDidDisappear:(PRPSplashScreen *)splashScreen;

@end

PRPSplashScreen builds its view in ‑loadView so you don’t have to drag a XIB file around every time you need it. This makes it a little easier to drop into projects. The view property is set to a single image view that fills the screen and centers its image.

BasicSplashScreen/PRPSplashScreen.m

- (void)loadView {

UIImageView *iv = [[UIImageView alloc] initWithImage:self.splashImage];

iv.autoresizingMask = UIViewAutoresizingFlexibleWidth |

UIViewAutoresizingFlexibleHeight;

iv.contentMode = UIViewContentModeCenter;

self.view = iv;

[iv release];

}

Now let’s take a look at the splashImage property. It’s writable, so if you want to set a custom transition image, you can. But you may just want to use Default.png as the splash image, since the whole point of this recipe is to create a smooth transition. So, we write a lazy initializer that loads Default.png by default. If you’re transitioning from your default image, you don’t need to touch this property. We use +[UIImage imageNamed:] to ensure an image with the appropriate scale (for example, Default@2x.png for Retina displays) is used.

BasicSplashScreen/PRPSplashScreen.m

- (UIImage *)splashImage {

if (splashImage == nil) {

self.splashImage = [UIImage imageNamed:@"Default.png"];

}

return splashImage;

}

Setting up the splash screen is easy: just present it as a modal view controller off your application’s root view controller. We’ll do this at launch time, before showing the main window but after adding the root view. This timing is important: the root view controller won’t properly present a modal view controller if its own view isn’t in place. In the BasicSplashScreen

Return Main Page Previous Page Next Page

®Online Book Reader