我正在尝试在包含我在不同类中使用的所有全局变量的类中初始化设备运动管理器。但是我似乎无法让这个工作。
我在全球课上有这个:
// Motion Manager
CMMotionManager *motionManager;
然后在不同的类头文件中设置该属性:
@property (retain) CMMotionManager *motionManager;
在.m文件中我合成并开始更新:
@synthesize motionManager;
motionManager = [[CMMotionManager alloc] init];
if (motionManager.deviceMotionAvailable)
{
motionManager.deviceMotionUpdateInterval = 1.0/100.0;
[motionManager startDeviceMotionUpdates];
NSLog(@"Device Started");
}
但是当我在我的第三堂课上打电话时:
motionManager.deviceMotionAvailable
返回NO。
PD:两个类都导入全局类,第三个类导入第二个类的标题。
答案 0 :(得分:0)
好吧,好像我可以使用单例类来实现这个目标:
h file:
//
// MotionManagerSingleton.h
// Air Interface
//
// Created by MacBook on 11/12/28.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
// File created using Singleton XCode Template by Mugunth Kumar (http://blog.mugunthkumar.com)
// More information about this template on the post http://mk.sg/89
// Permission granted to do anything, commercial/non-commercial with this file apart from removing the line/URL above
#import <Foundation/Foundation.h>
#import <CoreMotion/CoreMotion.h>
@interface MotionManagerSingleton : NSObject
+ (MotionManagerSingleton*) sharedInstance;
- (void)startMotionManager;
- (CMAcceleration)getCurrentAcceleration;
- (CMAttitude*)getCurrentAttitude;
- (CMAttitude*)getStartingAttitude;
- (void)setStartingAttitude: (CMAttitude*) ref;
- (bool)isDeviceReady;
- (void)destroyMotionManager;
- (NSTimeInterval) getStamp;
@end
m file:
//
// MotionManagerSingleton.m
// Air Interface
//
// Created by MacBook on 11/12/28.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
// File created using Singleton XCode Template by Mugunth Kumar (http://blog.mugunthkumar.com)
// More information about this template on the post http://mk.sg/89
// Permission granted to do anything, commercial/non-commercial with this file apart from removing the line/URL above
#import "MotionManagerSingleton.h"
@implementation MotionManagerSingleton
CMMotionManager *motionManager;
CMAttitude *referenceAttitude;
bool initialized;
#pragma mark -
#pragma mark Singleton Methods
+ (MotionManagerSingleton*)sharedInstance {
static MotionManagerSingleton *_sharedInstance;
if(!_sharedInstance) {
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_sharedInstance = [[super allocWithZone:nil] init];
});
}
return _sharedInstance;
}
+ (id)allocWithZone:(NSZone *)zone {
return [self sharedInstance];
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
#if (!__has_feature(objc_arc))
- (id)retain {
return self;
}
- (unsigned)retainCount {
return UINT_MAX; //denotes an object that cannot be released
}
- (void)release {
//do nothing
}
- (id)autorelease {
return self;
}
#endif
#pragma mark -
#pragma mark Custom Methods
// Add your custom methods here
- (void)startMotionManager{
if (!initialized) {
motionManager = [[CMMotionManager alloc] init];
if (motionManager.deviceMotionAvailable) {
motionManager.deviceMotionUpdateInterval = 1.0/70.0;
[motionManager startDeviceMotionUpdates];
NSLog(@"Device Motion Manager Started");
initialized = YES;
}
}
}
- (CMAcceleration)getCurrentAcceleration{
return motionManager.deviceMotion.userAcceleration;
}
- (CMAttitude*)getCurrentAttitude{
return motionManager.deviceMotion.attitude;
}
- (CMAttitude*)getStartingAttitude{
return referenceAttitude;
}
- (float)getInterval{
return motionManager.accelerometerUpdateInterval;
}
- (NSTimeInterval) getStamp{
return motionManager.deviceMotion.timestamp;
}
- (void)setStartingAttitude: (CMAttitude*) ref{
referenceAttitude = motionManager.deviceMotion.attitude;
}
- (bool)isDeviceReady{
return motionManager.deviceMotionActive;
}
- (void)destroyMotionManager{
[motionManager stopDeviceMotionUpdates];
motionManager = nil;
}
@end
每当我想使用它时,我只能在这个类头中声明一个变量:
MotionManagerSingleton *Manager;
并在m文件中使用它:
Manager = [MotionManagerSingleton sharedInstance];
if ([Manager isDeviceReady]) {
NSLog(@"Device Is Ready on Drawing View Controller");
}
referenceAttitude = [Manager getStartingAttitude];
答案 1 :(得分:0)
每个iOS应用程序中都有一个单例类,通常称为AppDelegate
:-)
这是因为AppDelegate
在技术上实现了UIApplicationDelegate
。
所以,我跟着this suggestion by Jonathan Hui,它对我来说很有效。
我希望这会有所帮助,而且答案还不算太晚......