Skip to content

Commit

Permalink
Merge pull request #6 from rafaelmaroxa/ra-accelerometer-updates
Browse files Browse the repository at this point in the history
Start/Stop accelerometer updates
  • Loading branch information
tastyone committed Apr 22, 2016
2 parents 68e3a9f + f27c26c commit 4c43bc0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 27 deletions.
3 changes: 3 additions & 0 deletions MotionOrientation.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ extern NSString* const kMotionOrientationKey;
+ (void)initialize;
+ (MotionOrientation *)sharedInstance;

- (void)startAccelerometerUpdates;
- (void)stopAccelerometerUpdates;

@end
70 changes: 43 additions & 27 deletions MotionOrientation.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ + (MotionOrientation *)sharedInstance
- (void)_initialize
{
self.showDebugLog = NO;

self.operationQueue = [[NSOperationQueue alloc] init];

self.motionManager = [[CMMotionManager alloc] init];
self.motionManager.accelerometerUpdateInterval = 0.1;
if ( ![self.motionManager isAccelerometerAvailable] ) {
Expand All @@ -62,9 +62,6 @@ - (void)_initialize
#endif
return;
}
[self.motionManager startAccelerometerUpdatesToQueue:self.operationQueue withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
[self accelerometerUpdateWithData:accelerometerData error:error];
}];
}

- (id)init
Expand All @@ -79,24 +76,24 @@ - (id)init
- (CGAffineTransform)affineTransform
{
int rotationDegree = 0;

switch (self.interfaceOrientation) {
case UIInterfaceOrientationPortrait:
rotationDegree = 0;
break;

case UIInterfaceOrientationLandscapeLeft:
rotationDegree = 90;
break;

case UIInterfaceOrientationPortraitUpsideDown:
rotationDegree = 180;
break;

case UIInterfaceOrientationLandscapeRight:
rotationDegree = 270;
break;

default:
break;
}
Expand All @@ -106,6 +103,25 @@ - (CGAffineTransform)affineTransform
return CGAffineTransformMakeRotation(MO_degreesToRadian(rotationDegree));
}

- (void)startAccelerometerUpdates
{
if (![self.motionManager isAccelerometerAvailable]) {
if ( self.showDebugLog ) {
NSLog(@"MotionOrientation - Accelerometer is NOT available");
}
return;
}

[self.motionManager startAccelerometerUpdatesToQueue:self.operationQueue withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
[self accelerometerUpdateWithData:accelerometerData error:error];
}];
}

- (void)stopAccelerometerUpdates
{
[self.motionManager stopAccelerometerUpdates];
}

- (void)accelerometerUpdateWithData:(CMAccelerometerData *)accelerometerData error:(NSError *)error
{
if ( error ) {
Expand All @@ -114,24 +130,24 @@ - (void)accelerometerUpdateWithData:(CMAccelerometerData *)accelerometerData err
}
return;
}

CMAcceleration acceleration = accelerometerData.acceleration;

// Get the current device angle
float xx = -acceleration.x;
float yy = acceleration.y;
float z = acceleration.z;
float angle = atan2(yy, xx);
float angle = atan2(yy, xx);
float absoluteZ = (float)fabs(acceleration.z);

// Add 1.5 to the angle to keep the label constantly horizontal to the viewer.
// [interfaceOrientationLabel setTransform:CGAffineTransformMakeRotation(angle+1.5)];
// [interfaceOrientationLabel setTransform:CGAffineTransformMakeRotation(angle+1.5)];

// Read my blog for more details on the angles. It should be obvious that you
// could fire a custom shouldAutorotateToInterfaceOrientation-event here.
UIInterfaceOrientation newInterfaceOrientation = self.interfaceOrientation;
UIDeviceOrientation newDeviceOrientation = self.deviceOrientation;

#ifdef DEBUG
NSString* orientationString = nil;
#endif
Expand Down Expand Up @@ -185,37 +201,37 @@ - (void)accelerometerUpdateWithData:(CMAccelerometerData *)accelerometerData err
orientationString = @"MotionOrientation - Right";
#endif
} else {

}

BOOL deviceOrientationChanged = NO;
BOOL interfaceOrientationChanged = NO;

if ( newDeviceOrientation != self.deviceOrientation ) {
deviceOrientationChanged = YES;
_deviceOrientation = newDeviceOrientation;
}

if ( newInterfaceOrientation != self.interfaceOrientation ) {
interfaceOrientationChanged = YES;
_interfaceOrientation = newInterfaceOrientation;
}

// post notifications
if ( deviceOrientationChanged ) {
[[NSNotificationCenter defaultCenter] postNotificationName:MotionOrientationChangedNotification
object:nil
[[NSNotificationCenter defaultCenter] postNotificationName:MotionOrientationChangedNotification
object:nil
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:self, kMotionOrientationKey, nil]];
if ( self.showDebugLog ) {
NSLog(@"didAccelerate: absoluteZ: %f angle: %f (x: %f, y: %f, z: %f), orientationString: %@",
absoluteZ, angle,
acceleration.x, acceleration.y, acceleration.z,
absoluteZ, angle,
acceleration.x, acceleration.y, acceleration.z,
orientationString);
}
}
if ( interfaceOrientationChanged ) {
[[NSNotificationCenter defaultCenter] postNotificationName:MotionOrientationInterfaceOrientationChangedNotification
object:nil
[[NSNotificationCenter defaultCenter] postNotificationName:MotionOrientationInterfaceOrientationChangedNotification
object:nil
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:self, kMotionOrientationKey, nil]];
}
}
Expand Down Expand Up @@ -246,7 +262,7 @@ - (void)dealloc
{
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] removeObserver:self];

#if __has_feature(objc_arc)
#else
[super dealloc];
Expand Down

0 comments on commit 4c43bc0

Please sign in to comment.