问题描述
我正在尝试初始化CMMotionManager,因此开始更新并创建参考姿态矩阵,然后当我单击情节提要中的按钮时,我显示另一个屏幕(位于视图控制器中),并且我想使用参考姿态矩阵和运动管理器提供的其他读数,但是如果我进行简单的检查以查看其是否准备就绪,则表示它尚未准备就绪(甚至认为它在先前的屏幕上已准备就绪).
I am trying to initialize the CMMotionManager so start updates and create a reference attitude matrix, and then when i click a button trough the storyboard, i display a different screen (which is in a viewcontroller) and i want to use the reference attitude matrix and other readings from the motion manager but if I do a simple check to see if its ready it says it isnt (even thought it was ready on the previous screen).
所以我在进行一些研究,发现一些斯坦福大学的笔记说这是一种全球资源,因此可以使用委托或类,这让我很想念.
So I was researching a little bit and i came across some standford notes that say it is a global resource so that it is ok to either use delegates or classes, that got me thinking.
如果我使用委托,那么从技术上讲,资源的所有者正在为我执行操作,对吗?所以这并不真的意味着它是全球性的.
If i use a delegate then technically the owner of the resource is doing the action for me right? so it doesnt really mean it is global.
那类呢?我尝试实现MotionManager类,但是我仍然没有在第二个屏幕上得到任何读数...而且我只是没有在新屏幕上再次初始化它,因为我不想同时运行多个实例.
What about classes? i tried implementing a MotionManager Class but i still didnt get any readings on the second screen... and i just didnt initialize it again on the new screen because i dont want to have multiple instances runing at the same time.
要解决我的问题,我还想我可以在主要对象中将其初始化一次,保存姿态,在segue过渡中将其传递给下一个对象,然后重新初始化运动管理器,这样我只有1个实例(或关闭屏幕上的旧内容将消失).
To solve my problem i also suppose i could initialize it once in the main one, save the attitude, pass it to the next one in a segue transition and just re initialize the motion manager so i only have 1 instance (or close old one on screen will dissapear).
但是我不希望这样,因为用户可能会立即在屏幕上单击,而运动管理器确实会花一点时间进行初始化(或者就是我注意到的那样).
But i do not want this because the user might click right away on the screen and the motion manager does take a little bit of time to initialize (or thats what i noticed).
在此先感谢您提供的任何帮助.
Thanks in advance for any help you can provide.
好吧,我尝试了委托方法,但仍然无法从第二个窗口中的设备管理器中读取内容.我想到的是,当我转到第二个窗口xcode时,也许会自动释放我的Motion管理器实例...(CMMotionManager).
Ok i tried the delegate method and i still cant read from the device manager in the second window. What comes to mind is that MAYBE when I am going to the second window xcode is automaticaly releasing my instance of Motion manager.... (CMMotionManager).
有人知道如何检查吗?
推荐答案
我希望我理解正确.然后,我建议使用单例设计模式将运动管理器访问封装在一个专门的类中.一些伪代码:
I hope I understood you right. Then I suggest using a singleton design pattern encapsulating motion manager access in a specialised class. Some pseudo code:
MotionHandler.h
MotionHandler.h
@interface MotionHandler {
CMMotionManager* motionManager;
+ (MotionHandler*) getInstance;
}
MotionHandler.c:
MotionHandler.c:
@interface MotionHandler {
static MotionHandler* instance;
+ (MotionHandler*) getInstance {
if (instance == nil) {
instance = [[self alloc] init];
}
return instance;
}
- (id)init {
if ((self = [super init])) {
motionManager = [[CMMotionManager alloc] init];
// initialise CMMotionManager
}
}
}
因此,只有一个MotionHandler实例可以管理对CMMotionManager的访问.您可以使用 MotionHandler.getInstance.motionManager
从任何地方访问CMMotionManager实例.
So there is one instance of MotionHandler only which manages access to CMMotionManager. You can accesss your CMMotionManager instance from everywhere with MotionHandler.getInstance.motionManager
.
如果您需要从多个类访问CoreMotion,建议您完全封装CMMotionManager访问权限.这意味着将其设为@private并提供诸如getDeviceMotion,setReferenceAttitude等方法.这有助于避免诸如两次启动或在启动之前访问CMDeviceMotion之类的麻烦,并使调试更加方便.
If you need access to CoreMotion from several classes, I recommend total encapsulation of CMMotionManager access. That means make it @private and provide methods like getDeviceMotion, setReferenceAttitude, ...This helps to avoid complications like starting it twice or access to CMDeviceMotion before start and makes it more convenient to debug.
这篇关于CMMotionManager是全局资源吗?这是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!