iOS陀螺仪使虚拟地平线

doc*_*doc 6 ios gyroscope

我希望使用陀螺仪为我的应用程序添加虚拟地平线,就像这个

我在陀螺仪上发现了很多文档,我认为我使用偏航但我不知道如何用重力进行校准.

有谁可以帮助我吗 ??

谢谢.

doc*_*doc 7

我用这个解决了我的问题:

- (void)updateImage: (NSNumber *)rotNum
{
    self.image.transform = CGAffineTransformMakeRotation([rotNum floatValue]);
}

// call the above function to rotate the image in reference to the horizon
[motionManager startDeviceMotionUpdatesToQueue: gyroQueue
                                   withHandler: ^(CMDeviceMotion *motion, NSError *error) {
                                                       [self performSelectorOnMainThread: @selector(updateImage:)
                                                                              withObject: [NSNumber numberWithDouble: atan2(motion.gravity.x, motion.gravity.y)-M_PI]
                                                                           waitUntilDone: NO];
                                               }];
Run Code Online (Sandbox Code Playgroud)