Last active
December 20, 2015 01:49
-
-
Save zapsleep/6051762 to your computer and use it in GitHub Desktop.
Source of main view controller in DeviceMotionTest project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "DVViewController.h" | |
#import <CoreMotion/CoreMotion.h> | |
#define kCMDeviceMotionUpdateFrequency (1.f/30.f) | |
@interface DVViewController () | |
@property (weak, nonatomic) IBOutlet UILabel *accelerationXVal; | |
@property (weak, nonatomic) IBOutlet UILabel *accelerationYVal; | |
@property (weak, nonatomic) IBOutlet UILabel *accelerationZVal; | |
@property (weak, nonatomic) IBOutlet UILabel *gravityXVal; | |
@property (weak, nonatomic) IBOutlet UILabel *gravityYVal; | |
@property (weak, nonatomic) IBOutlet UILabel *gravityZVal; | |
@property (weak, nonatomic) IBOutlet UILabel *rotationXVal; | |
@property (weak, nonatomic) IBOutlet UILabel *rotationYVal; | |
@property (weak, nonatomic) IBOutlet UILabel *rotationZVal; | |
@property (weak, nonatomic) IBOutlet UILabel *pitch; | |
@property (weak, nonatomic) IBOutlet UILabel *roll; | |
@property (weak, nonatomic) IBOutlet UILabel *yaw; | |
@property (nonatomic, strong) CMMotionManager *motionManager; | |
@property (nonatomic, strong) CADisplayLink *displayLink; | |
@end | |
@implementation DVViewController | |
-(CMMotionManager *)motionManager { | |
if (!_motionManager) { | |
_motionManager = [[CMMotionManager alloc] init]; | |
_motionManager.deviceMotionUpdateInterval = kCMDeviceMotionUpdateFrequency; | |
} | |
return _motionManager; | |
} | |
-(CADisplayLink *)displayLink { | |
if (!_displayLink) { | |
_displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateMotionData)]; | |
} | |
return _displayLink; | |
} | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view, typically from a nib. | |
[self.motionManager startDeviceMotionUpdates]; | |
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; | |
} | |
- (void)didReceiveMemoryWarning | |
{ | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
- (void)updateMotionData { | |
//1 | |
CMAcceleration acceleration = self.motionManager.deviceMotion.userAcceleration; | |
self.accelerationXVal.text = [NSString stringWithFormat:@"%.3f", acceleration.x]; | |
self.accelerationYVal.text = [NSString stringWithFormat:@"%.3f", acceleration.y]; | |
self.accelerationZVal.text = [NSString stringWithFormat:@"%.3f", acceleration.z]; | |
// | |
//2 | |
CMAcceleration gravity = self.motionManager.deviceMotion.gravity; | |
self.gravityXVal.text = [NSString stringWithFormat:@"%.3f", gravity.x]; | |
self.gravityYVal.text = [NSString stringWithFormat:@"%.3f", gravity.y]; | |
self.gravityZVal.text = [NSString stringWithFormat:@"%.3f", gravity.z]; | |
// | |
//3 | |
CMRotationRate rotation = self.motionManager.deviceMotion.rotationRate; | |
self.rotationXVal.text = [NSString stringWithFormat:@"%.3f", rotation.x]; | |
self.rotationYVal.text = [NSString stringWithFormat:@"%.3f", rotation.y]; | |
self.rotationZVal.text = [NSString stringWithFormat:@"%.3f", rotation.z]; | |
// | |
//4 | |
CMAttitude *attitude = self.motionManager.deviceMotion.attitude; | |
self.pitch.text = [NSString stringWithFormat:@"%.3f", attitude.pitch]; | |
self.roll.text = [NSString stringWithFormat:@"%.3f", attitude.roll]; | |
self.yaw.text = [NSString stringWithFormat:@"%.3f", attitude.yaw]; | |
// | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment