Guest User

Untitled

a guest
May 24th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. static UIDeviceOrientation previousOrientation = UIDeviceOrientationPortrait;
  2.  
  3. - (void)applicationDidFinishLaunching:(UIApplication *)application {
  4. [window addSubview:viewController.view];
  5. [window makeKeyAndVisible];
  6. [[NSNotificationCenter defaultCenter] addObserver:self
  7. selector:@selector(didRotate:)
  8. name:@"UIDeviceOrientationDidChangeNotification" object:nil];
  9.  
  10. }
  11.  
  12. - (void) didRotate:(NSNotification *)notification{
  13. UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
  14.  
  15. [self doRotationStuff:orientation: previousOrientation];
  16. previousOrientation = orientation;
  17.  
  18. }
  19.  
  20. #define kUpdateFrequency 30 // Hz
  21. #define kUpdateCount 15 // So we init after half a second
  22. #define kFilteringFactor (1.0f / kUpdateCount)
  23.  
  24. - (void)applicationDidFinishLaunching:(UIApplication *)app
  25. {
  26. [UIAccelerometer sharedAccelerometer].updateInterval = (1.0 / kUpdateFrequency);
  27. [UIAccelerometer sharedAccelerometer].delegate = self;
  28. accelerometerCounter = 0;
  29. ...
  30. }
  31.  
  32. - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)accel
  33. {
  34. // Average out the first kUpdateCount readings
  35. // acceleration_[xyz] are ivars typed float
  36. acceleration_x = (float)accel.x * kFilteringFactor + acceleration_x * (1.0f - kFilteringFactor);
  37. acceleration_y = (float)accel.y * kFilteringFactor + acceleration_y * (1.0f - kFilteringFactor);
  38. acceleration_z = (float)accel.z * kFilteringFactor + acceleration_z * (1.0f - kFilteringFactor);
  39.  
  40. accelerometerCounter++;
  41. if (accelerometerCounter == kUpdateCount)
  42. {
  43. [self initOrientation];
  44. [UIAccelerometer sharedAccelerometer].delegate = nil;
  45. }
  46. }
  47.  
  48. - (void)initOrientation
  49. {
  50. // Figure out orientation from acceleration_[xyz] and set up your UI...
  51. }
  52.  
  53. [self performSelector:@selector(getOriented) withObject:nil afterDelay:0.0f];
  54.  
  55. [self performSelector:@selector(getOriented) withObject:nil afterDelay:0.0f];
  56.  
  57. - (void) getOriented
  58. {
  59. UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
  60. // save orientation somewhere
  61. }
  62.  
  63. -(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {}
Add Comment
Please, Sign In to add comment