The Code That Breaks

Một phần của tài liệu beginning iphone 3 development exploring the iphone sdk phần 9 pot (Trang 47 - 50)

Create a new project in Xcode using the view-based application template. Call the new proj- ect ShakeAndBreak. In the 15 ShakeAndBreak folder of the project archive, we’ve provided you the two images and the sound file you need for this application, so drag home.png, homebroken.png, and glass.wav to the Resources folder of your project. There’s also an icon.

png in that folder. Add that to the Resources folder as well.

note

Just for completeness, we’ve included a modified version of Shake and Break in the project archives based on the 3.0 shake detection method. You’ll find it in the project archive in a folder named 15 ShakeAndBreak - Motion Methods. The magic is in ShakeAndBreakViewController’s

motionEnded:withEvent: method.

Next, expand the Resources folder, and single-click ShakeAndBreak-Info.plist. We need to add an entry to the property list to tell our application not to use a status bar, so single-click the row that says Information Property List, and click the button that appears at the end of the row to add a new child. Change the new row’s Key to UIStatusBarHidden. After you change the key, the Value column should change to a checkbox. If it doesn’t change automatically, control-click (or right-click if you have a two-button mouse) the empty Value column in the row you just added. A contextual menu should appear (see Figure 15-5). From that menu, select the Value type submenu, and then select Boolean. Now, click the checkbox so that it is checked. Finally, type icon.png in the Value column next to the Icon file key.

Figure 15-5. Changing the Value Type for UIStatusBarHidden

Now, expand the Classes folder. We’re going to need to create an outlet to point to an image view so that we can change the displayed image. We’ll also need a couple of UIImage

instances to hold the two pictures, a sound ID to refer to the sound, and a Boolean to keep track of whether the screen needs to be reset. Single-click ShakeAndBreakViewController.h, and add the following code:

#import <UIKit/UIKit.h>

#import <AudioToolbox/AudioToolbox.h>

#define kAccelerationThreshold 2.2

#define kUpdateInterval (1.0f/10.0f)

@interface ShakeAndBreakViewController :

UIViewController <UIAccelerometerDelegate> { UIImageView *imageView;

BOOL brokenScreenShowing;

SystemSoundID soundID;

UIImage *fixed;

UIImage *broken;

}

@property (nonatomic, retain) IBOutlet UIImageView *imageView;

@property (nonatomic, retain) UIImage *fixed;

@property (nonatomic, retain) UIImage *broken;

@end

Save the header file, and double-click ShakeAndBreakViewController.xib to open the file in Interface Builder. Single-click the View icon. First, press 1 to bring up the attribute inspec- tor, and change the Status Bar pop-up under Simulated User Interface Elements from Gray to None. Now, drag an Image View over from the library to the window labeled View. The image view should automatically resize to take up the full window, so just place it so that it sits per- fectly within the window.

Control-drag from the File’s Owner icon to the image view, and select the imageView outlet.

Now, save and close the nib file, and go back to Xcode. When you get there, single-click the ShakeAndBreakController.m file, and add the following code at the top of the file:

#import "ShakeAndBreakViewController.h"

@implementation ShakeAndBreakViewController

@synthesize imageView;

@synthesize fixed;

@synthesize broken;

- (void) viewDidLoad {

UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];

accel.delegate = self;

accel.updateInterval = kUpdateInterval;

NSString *path = [[NSBundle mainBundle] pathForResource:@"glass"

ofType:@"wav"];

AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);

self.fixed = [UIImage imageNamed:@"home.png"];

self.broken = [UIImage imageNamed:@"homebroken.png"];

imageView.image = fixed;

} ...

Insert the following lines of code into the existing dealloc and viewDidUnload methods:

...

- (void)viewDidUnload {

// Release any retained subviews of the main view.

// e.g. self.myOutlet = nil;

self.imageView = nil;

self.fixed = nil;

self.broken = nil;

[super viewDidUnload];

}

- (void)dealloc {

[imageView release];

[fixed release];

[broken release];

[super dealloc];

} ...

And add the following new methods at the bottom of the file:

...

#pragma mark -

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { if (!brokenScreenShowing) {

if (acceleration.x > kAccelerationThreshold || acceleration.y > kAccelerationThreshold || acceleration.z > kAccelerationThreshold) { imageView.image = broken;

AudioServicesPlaySystemSound(soundID);

brokenScreenShowing = YES;

} } }

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { imageView.image = fixed;

brokenScreenShowing = NO;

}

@end

The first method we implement is viewDidLoad, where we get a reference to the shared accelerometer instance, set self to be the accelerometer’s delegate, and then set the update frequency using the constant we defined earlier:

UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];

accel.delegate = self;

accel.updateInterval = kUpdateInterval;

Một phần của tài liệu beginning iphone 3 development exploring the iphone sdk phần 9 pot (Trang 47 - 50)

Tải bản đầy đủ (PDF)

(58 trang)