CMAccelerometerData类V.不推荐使用UIAccelerometer类 [英] CMAccelerometerData class Vs. deprecated UIAccelerometer class

查看:483
本文介绍了CMAccelerometerData类V.不推荐使用UIAccelerometer类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文档中, CMAccelerometerData类(位于Core Motion框架中)具有 CMAcceleration 类型的属性 加速 是一个包含3个值(双x,双y,双z)的结构的typedef

Based on this documentation, the CMAccelerometerData class (found in the Core Motion framework) has a property of type CMAcceleration called acceleration that is a typedef of a struct containing 3 values (double x, double y, double z)

我对Objective-C非常陌生)所以我的问题是这样:我的访问,我们说在属性中的 double y 值,在我的代码的某一点?

I'm rather new to Objective-C (I only know C++..) so my question is this : How do I access, let's say the double y value kept in that property, at some point during my code?

我首先如下创建CMAccelerometerData类的实例:

Do I first create an instance of the CMAccelerometerData class like this :

CMAccelerometerData *myAccelerometer;

然后访问其加速属性:

double axisYvalue = [myAccelerometer acceleration]; 

上面显然是错误的,不是吗?我必须得到Y在加速专门发现,那么我该怎么做?

the above is obviously wrong, isn't it? I have to get the Y found in acceleration specifically so how do I do that?

double axisYvalue = [myAccelerometer acceleration->y]; // no this is wrong as well..

那么我该如何做呢?

最后一个问题,如果我可以:)

And one last question if I may :)

给我这个特定的类和属性,现在每次,在我的代码,我使用像

given this specific class and property that I mentioned.. and let's say I've instantiated my CMAccelerometer class.. Now every time, during my code, I use something like

return [myAccelerometer acceleration->y]; // let's say that's the correct version :)

的具体时刻获取Y轴的值?

inside some -(double) method .. will I be getting the value of the Y-axis at that specific moment in which the call is being made ?

我问这个,因为我读到有关现在已经过时的UIAccelerometer类,你不得不定义时间间隔,并更新x,y,z的值经常等等等。
现在我可以得到值在加速度属性的调用时刻在Y轴上被执行,不是这种情况?

I am asking this because I got confused when reading about the now deprecated UIAccelerometer class where you had to define intervals and update the values of x,y,z every so often etc.. where as now I can get the value that is being exercised on the Y-axis the moment the call to the acceleration property is made, isn't that the case?

phew ...抱歉的长度的文本! :)

phew... sorry for the length of this text! :)

推荐答案

从C ++开始,我认为可以放心地理解指针。第一行:

Coming from C++, I assume it's safe to assume that you understand pointers. That first line:

CMAccelerometerData *myAccelerometer;

...不是创建实例声明指向实例的指针,它不会指向任何有效的。要获得有效的实例,你永远不会自己创建一个。相反,您需要使用 CMMotionManager < a> class' accelerometerData 属性获取指向有效对象的指针:

...isn't creating an instance, it's declaring a pointer to an instance, which won't point to anything valid. To get a valid instance, you'll never actually create one of these yourself. Instead, you'll use the CMMotionManager class' accelerometerData property to get a pointer to a valid object:

// Sometime earlier...
CMMotionManager* manager = [[CMMotionManager alloc] init];
[manager startAccelerometerUpdates];

// Sometime in the present...
// Get a ref to the most recent accelerometer data.
CMAccelerometerData* data = [manager accelerometerData];

// Access it.
double x = [data acceleration].x;

这篇关于CMAccelerometerData类V.不推荐使用UIAccelerometer类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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