How to replace UIAccelerometer with CMMotionManage

2019-02-19 01:09发布

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. warnings

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. problems

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!

1条回答
趁早两清
2楼-- · 2019-02-19 01:48

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];
    }];

}
查看更多
登录 后发表回答