Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static UIDeviceOrientation previousOrientation = UIDeviceOrientationPortrait;
- - (void)applicationDidFinishLaunching:(UIApplication *)application {
- [window addSubview:viewController.view];
- [window makeKeyAndVisible];
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(didRotate:)
- name:@"UIDeviceOrientationDidChangeNotification" object:nil];
- }
- - (void) didRotate:(NSNotification *)notification{
- UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
- [self doRotationStuff:orientation: previousOrientation];
- previousOrientation = orientation;
- }
- #define kUpdateFrequency 30 // Hz
- #define kUpdateCount 15 // So we init after half a second
- #define kFilteringFactor (1.0f / kUpdateCount)
- - (void)applicationDidFinishLaunching:(UIApplication *)app
- {
- [UIAccelerometer sharedAccelerometer].updateInterval = (1.0 / kUpdateFrequency);
- [UIAccelerometer sharedAccelerometer].delegate = self;
- accelerometerCounter = 0;
- ...
- }
- - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)accel
- {
- // Average out the first kUpdateCount readings
- // acceleration_[xyz] are ivars typed float
- acceleration_x = (float)accel.x * kFilteringFactor + acceleration_x * (1.0f - kFilteringFactor);
- acceleration_y = (float)accel.y * kFilteringFactor + acceleration_y * (1.0f - kFilteringFactor);
- acceleration_z = (float)accel.z * kFilteringFactor + acceleration_z * (1.0f - kFilteringFactor);
- accelerometerCounter++;
- if (accelerometerCounter == kUpdateCount)
- {
- [self initOrientation];
- [UIAccelerometer sharedAccelerometer].delegate = nil;
- }
- }
- - (void)initOrientation
- {
- // Figure out orientation from acceleration_[xyz] and set up your UI...
- }
- [self performSelector:@selector(getOriented) withObject:nil afterDelay:0.0f];
- [self performSelector:@selector(getOriented) withObject:nil afterDelay:0.0f];
- - (void) getOriented
- {
- UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
- // save orientation somewhere
- }
- -(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {}
Add Comment
Please, Sign In to add comment