ShakeAndBreakViewController.m
上传用户:jjjjag8
上传日期:2017-04-17
资源大小:1443k
文件大小:2k
源码类别:

iPhone

开发平台:

MultiPlatform

  1. #import "ShakeAndBreakViewController.h"
  2. @implementation ShakeAndBreakViewController
  3. @synthesize imageView;
  4. @synthesize fixed;
  5. @synthesize broken;
  6. - (void) viewDidLoad {
  7. UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
  8. accel.delegate = self;
  9. accel.updateInterval = kUpdateInterval;
  10. NSString *path = [[NSBundle mainBundle] pathForResource:@"glass" ofType:@"wav"];
  11. AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);
  12. self.fixed = [UIImage imageNamed:@"home.png"];
  13. self.broken = [UIImage imageNamed:@"homebroken.png"];
  14. imageView.image = fixed;
  15. brokenScreenShowing = NO;
  16. }
  17. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  18. // Return YES for supported orientations
  19. return (interfaceOrientation == UIInterfaceOrientationPortrait);
  20. }
  21. - (void)didReceiveMemoryWarning {
  22. [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
  23. // Release anything that's not essential, such as cached data
  24. }
  25. - (void)dealloc {
  26. [imageView release];
  27. [fixed release];
  28. [broken release];
  29. [super dealloc];
  30. }
  31. - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
  32. if (! brokenScreenShowing) {
  33. if (fabsf(acceleration.x) > kAccelerationThreshold || fabsf(acceleration.y) > kAccelerationThreshold || fabsf(acceleration.z) > kAccelerationThreshold) {
  34. imageView.image = broken;
  35. AudioServicesPlaySystemSound (soundID);
  36. brokenScreenShowing = YES;
  37. }
  38. }
  39. }
  40. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  41. {
  42. imageView.image = fixed;
  43. brokenScreenShowing = NO;
  44. }
  45. @end