1
내 iPhone 4S의 피치, 롤 및 요를 기록하기위한 간단한 코드를 얻으려고합니다. 타이머가 올바르게 작동하지만 pitch, roll, yaw 값은 항상 콘솔에 0.0000000으로 표시됩니다. 내가 여기서 무엇을 놓치고 있니? 어떤 도움을 주셔서 감사합니다!deviceMotion이 피치, 롤 또는 요에 대해 아무것도 반환하지 않음
@interface ViewController : UIViewController
@property (nonatomic, retain) CMMotionManager *motionManager;
@property(readonly, weak) CMDeviceMotion *deviceMotion;
-(void)doSomething;
@end
그리고 여기 내하는 .m 파일입니다 : 당신은 _motionManager
에 아무것도 할당 할하지 않는 것
#import "ViewController.h"
@implementation ViewController
@synthesize motionManager = _motionManager;
@synthesize deviceMotion = _deviceMotion;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[_motionManager startDeviceMotionUpdates];
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(doSomething) userInfo:nil repeats:YES];
}
- (void)doSomething
{
NSLog(@"Reading Device Motion Updates..");
_deviceMotion = [_motionManager deviceMotion];
NSLog(@"Roll = %f, Pitch = %f, Yaw = %f", _deviceMotion.attitude.roll, _deviceMotion.attitude.pitch, _deviceMotion.attitude.yaw);
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
[_motionManager stopDeviceMotionUpdates];
}
@end
저는 .h 파일로 생성합니다 : @property (nonatomic, retain) CMMotionManager * motionManager; viewDidLoad에서 시작하도록 지시 : [_motionManager startDeviceMotionUpdates]; 충분하지 않습니까? – Luke
아하, 나는 alloc'd하지 않았다. 지금 정렬되었습니다. – Luke
헤더 파일에 생성하지 않습니다. '@ property'는 당신이 그 속성을 통해 멤버 변수에 접근 할 수 있다고 선언합니다. '@ synthesize'는 멤버 변수에 대한 액세스를 제공하는 메소드를 생성합니다. 하지만 실제로 인스턴스 변수/속성에 객체를 만들고 할당해야합니다. 그렇지 않으면 액세스 할 수있는 게 없습니다. – Jim