2011-12-31 43 views
6

Tôi nghĩ rằng tôi chỉ bắt đầu giá trị UIAcceleration y và so sánh nó với giá trị hiện tại và sau đó 'đo' nhưng tôi không biết chính xác nếu (1) đúng (2) toán đó là gì.Làm cách nào để xác định góc của thiết bị?

Cảm ơn bạn đã được trợ giúp.

Cập nhật:

Đây là mã tôi đã sử dụng theo đề xuất của jrturton.

#import "ViewController.h" 

@interface ViewController() 
{ 
    CMMotionManager *motionManager; 
} 
@end 

@implementation ViewController 
@synthesize angleLabel; 

- (void)startMotion 
{ 
    if (motionManager == nil) 
    { 
    motionManager = [[CMMotionManager alloc] init]; 
    } 
    motionManager = [[CMMotionManager alloc] init]; 
    [motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) { 
    angleLabel.text = [NSString stringWithFormat:@"%g", motion.attitude.pitch]; 
    }]; 
} 

- (void) stopMotion 
{ 
    if (motionManager) 
    { 
    [motionManager stopDeviceMotionUpdates]; 
    } 

    motionManager = nil; 
} 

- (void)viewDidUnload { 
    [self setAngleLabel:nil]; 
    [super viewDidUnload]; 
} 
@end 

Trả lời

4

Dễ hơn thế. Sử dụng thuộc tính attitude của CMDeviceMotion. Xem here để biết tài liệu.

+0

Cảm ơn, tôi đã làm việc đó. Sẽ đính kèm mã trong câu hỏi ban đầu. – cynicaljoy

Các vấn đề liên quan