gpt4 book ai didi

ios - CMMotionManager vs UIAccelerometer 效率

转载 作者:可可西里 更新时间:2023-11-01 05:05:57 25 4
gpt4 key购买 nike

我一直在研究 AR framework现在有一段时间了,我正在努力update from UIAccelerometer (deprecated) to CMMotionManager但遇到了一些效率问题?

基本上,CMMotionManager 似乎比 UIAccelerometer 更大更慢。以前有人遇到过 CMMotionManager 的性能问题吗?


如你所见here ,我有这个:

accelerometer = [UIAccelerometer sharedAccelerometer];
accelerometer.updateInterval = 0.01;
[accelerometer setDelegate:self];

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
rollingZ = (acceleration.z * kFilteringFactor) + (rollingZ * (1.0 - kFilteringFactor));
rollingX = (acceleration.y * kFilteringFactor) + (rollingX * (1.0 - kFilteringFactor));

if (rollingZ > 0.0) currentInclination = inc_avg(atan(rollingX / rollingZ) + M_PI / 2.0);
else if (rollingZ < 0.0) currentInclination = inc_avg(atan(rollingX / rollingZ) - M_PI / 2.0);
else if (rollingX < 0) currentInclination = inc_avg(M_PI/2.0);
else if (rollingX >= 0) currentInclination = inc_avg(3 * M_PI/2.0);
}

即使在像 iPhone 4 这样的“旧”设备上也能正常工作(不是很旧,但是是的......)。

但是当使用 CMMotionManager 尝试完全相同的代码时:

motionManager = [[CMMotionManager alloc] init];

[motionManager setAccelerometerUpdateInterval:0.01];
[motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue]
withHandler: ^(CMAccelerometerData *accelerometerData, NSError *error){

rollingZ = (accelerometerData.acceleration.z * kFilteringFactor) + (rollingZ * (1.0 - kFilteringFactor));
rollingX = (accelerometerData.acceleration.y * kFilteringFactor) + (rollingX * (1.0 - kFilteringFactor));

if (rollingZ > 0.0) currentInclination = inc_avg(atan(rollingX / rollingZ) + M_PI / 2.0);
else if (rollingZ < 0.0) currentInclination = inc_avg(atan(rollingX / rollingZ) - M_PI / 2.0);
else if (rollingX < 0) currentInclination = inc_avg(M_PI/2.0);
else if (rollingX >= 0) currentInclination = inc_avg(3 * M_PI/2.0);
}];

数学似乎减慢了废话的速度……!我这样说是因为当我删除所有数学部分时效果很好。

iPhone 5 可以正常工作,但 iPhone 4S 会出现延迟迹象,而 iPhone 4 会死机...

(如果你愿意,我可以给你更多细节,但解释起来相对复杂)

最佳答案

我刚刚遇到了同样的问题,你不知道吗,解决方案在文档中;)

问题出在 block 格式上。所有教程似乎都支持这种方法,但 Apple 建议定期轮询 CMMotionManager 作为一种更注重性能的方法。 block 格式增加了开销。

来自 CMMotionManager 类引用:

To handle motion data by periodic sampling, the app calls a “start” method taking no arguments and periodically accesses the motion data held by a property for a given type of motion data. This approach is the recommended approach for apps such as games. Handling accelerometer data in a block introduces additional overhead, and most game apps are interested only the latest sample of motion data when they render a frame.

那么你想做什么,再次来自文档:

Call startAccelerometerUpdates to begin updates and periodically access CMAccelerometerData objects by reading the accelerometerData property.

类似的东西

CMMotionManager *mManager = [(AppDelegate *)[[UIApplication sharedApplication] delegate] sharedManager];

[mManager startAccelerometerUpdates];

然后,以您选择的某种定期更新方法:

CMMotionManager *mManager = [(SEPAppDelegate *)[[UIApplication sharedApplication] delegate] sharedManager];

CMAccelerometerData *aData = mManager.accelerometerData;

根据我所做的有限测试,此解决方案在 iPhone 4 上似乎与 UIAccelerometer 一样有效。

关于ios - CMMotionManager vs UIAccelerometer 效率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17659352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com