How to replace UIAccelerometer with CMMotionManager?

岁酱吖の 提交于 2019-12-01 07:42:37

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/20340384/how-to-replace-uiaccelerometer-with-cmmotionmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!