Pregunta

I'm using the [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications] to get notifications for when the device orientation is UIDeviceOrientationFaceDown.

It works great except for when device orientation is locked.

Is there any way to implement the same behavior using the Core Motion Framework Gyroscope ... ?

¿Fue útil?

Solución

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.motionUpdatesQueue = [NSOperationQueue new];
    self.motionUpdatesQueue.maxConcurrentOperationCount = 1;
    self.motionManager = [CMMotionManager new];
    [self.motionManager startDeviceMotionUpdatesToQueue:self.motionUpdatesQueue withHandler:^(CMDeviceMotion *motion, NSError *error) {
        [self handleMotion:motion error:error];
    }];
    return YES;
}

- (void)handleMotion:(CMDeviceMotion *)motion error:(NSError *)error {
    double diff = fabs(1.0 - motion.gravity.z);
    if(diff < .1) {
        NSLog(@"FaceDown");
    } else {
        NSLog(@"non FaceDown");
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top