如何用 CMMotionManager 替换 UIAccelerometer? [英] How to replace UIAccelerometer with CMMotionManager?

查看:25
本文介绍了如何用 CMMotionManager 替换 UIAccelerometer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 iOS 开发的新手.

我按照 .我添加了一些小功能,以便您可以手动添加不在 Google 数据库中的位置,但我认为不是这些功能导致了问题.

提前感谢您的帮助!

解决方案

试试这个链接:https://www.inkling.com/read/learning-ios-programming-alasdair-allan-2nd/chapter-9/the-core-运动框架

我正在学习一些用 UIAccelerometer 翻译的花絮

[self setAccelometerManager [UIAccelerometer sharedAccelerometer]];

可能变成

[self.motionManager = [[CMMotionManager alloc] init];

设置手动更新间隔,如

[[self accelerometerManager] setUpdateInterval: 0.25];

你可以拥有

self.motionManager.acceleratorUpdateInterval = 0.25;

并释放委托

self.accelerometerManager.delegate = nil;

现在是

[self.motionManager stopDeviceMotionUpdates];

同样从链接中,我最终做了这样的事情:

motionManager = [[CMMotionManager alloc] init];motionManager.acceleratorUpdateInterval = 1.0/10.0;//以 10Hz 更新如果(motionManager.acceleratorAvailable){queue = [NSOperationQueue currentQueue];[motionManager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {开关(当前方向){案例 UIDeviceOrientationLandscapeLeft:viewAngle = atan2(accelerometerData.acceleration.x, accelerometerData.acceleration.z);休息;案例 UIDeviceOrientationLandscapeRight:viewAngle = atan2(-accelerometerData.acceleration.x, accelerometerData.acceleration.z);休息;案例 UIDeviceOrientationPortrait:viewAngle = atan2(accelerometerData.acceleration.y, accelerometerData.acceleration.z);休息;案例 UIDeviceOrientationPortraitUpsideDown:viewAngle = atan2(-accelerometerData.acceleration.y, accelerometerData.acceleration.z);休息;默认:休息;}[自更新中心坐标];}];}

I'm new to iOS development.

I follow the tutorial from Ray Wenderlich to create a little location based AR app. However, the tutorial uses an AR Toolkit which has not been updated for a while. The UIAccelerometer it uses has already been deprecated since iOS 5, so when I try to run it on my iPhone (iOS 7.0.4), the Xcode says that there are 3 warnings, and all of them are caused by UIAccelerometer.

The result it leads to is that all the marks stay at the center of the screen one above another, and the tilt does not work at all.

According to my research, I guess what I need to do is to use CMMotionManager instead of UIAccelerometer, but as I said before, I'm totally new to iOS development and have no idea how to replace it.

Here is the source code. I add some little functions such that you can manually add locations that are not in the Google database, but I don't think it is these functions that result in the problem.

Thanks for you help in advance!

解决方案

Try this link: https://www.inkling.com/read/learning-ios-programming-alasdair-allan-2nd/chapter-9/the-core-motion-framework

I'm learning a few tidbits that translate some-what with the UIAccelerometer

i.e.

[self setAccelometerManager [UIAccelerometer sharedAccelerometer]];

could become

[self.motionManager = [[CMMotionManager alloc] init];

Setting manual update intervals like

[[self accelerometerManager] setUpdateInterval: 0.25];

you can have

self.motionManager.accelerometerUpdateInterval = 0.25;

and releasing the delegate

self.accelerometerManager.delegate = nil;

would now be

[self.motionManager stopDeviceMotionUpdates];

Also from the link, I ended up doing something like this:

motionManager = [[CMMotionManager alloc] init];

motionManager.accelerometerUpdateInterval  = 1.0/10.0; // Update at 10Hz

if (motionManager.accelerometerAvailable) {
    queue = [NSOperationQueue currentQueue];
    [motionManager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
        switch (currentOrientation) {
            case UIDeviceOrientationLandscapeLeft:
                viewAngle = atan2(accelerometerData.acceleration.x, accelerometerData.acceleration.z);
                break;
            case UIDeviceOrientationLandscapeRight:
                viewAngle = atan2(-accelerometerData.acceleration.x, accelerometerData.acceleration.z);
                break;
            case UIDeviceOrientationPortrait:
                viewAngle = atan2(accelerometerData.acceleration.y, accelerometerData.acceleration.z);
                break;
            case UIDeviceOrientationPortraitUpsideDown:
                viewAngle = atan2(-accelerometerData.acceleration.y, accelerometerData.acceleration.z);
                break;  
            default:
                break;
        }
        [self updateCenterCoordinate];
    }];

}

这篇关于如何用 CMMotionManager 替换 UIAccelerometer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆