/* From Beginning iPhone Development 3 by Dave Mark
/* Not gonna tell ya what this does! Implement it and shake your phone (or simulator) to see :) */
/* in view Controller.h*/
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#define kAccelerationThreshold 2.2
#define kUpdateInterval (1.0f/10.f)
@interface _5_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
/*in ViewController.m */
#import "_5_ShakeAndBreakViewController.h"
@implementation _5_ShakeAndBreakViewController
@synthesize imageView;
@synthesize fixed;
@synthesize broken;
- (void) viewDidLoad {
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;
[self.view becomeFirstResponder];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
- (BOOL) canBecomeFirstResponder {
return YES;
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.imageView = nil;
self.fixed = nil;
self.broken = nil;
}
- (void)dealloc {
[imageView release];
[fixed release];
[broken release];
[super dealloc];
}
#pragma mark Touch Handling
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
imageView.image = fixed;
brokenScreenShowing = NO;
}
#pragma mark Motion Handling
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
imageView.image = broken;
AudioServicesPlaySystemSound (soundID);
brokenScreenShowing = YES;
[super motionEnded:motion withEvent:event];
}
@end
/* in your xib */
/* Hide the status bar. Drag an image view and allow it to take up whole screen. control drag from file owner to image view and select imageView*/