From 5acac436f90506a023b94421c41c8c3a17f93fbc Mon Sep 17 00:00:00 2001 From: Dhilan007 Date: Tue, 8 Oct 2013 10:11:52 +0800 Subject: [PATCH] issues #2677:Use CoreMotion's API to replace UIAccelerometer. --- cocos2dx/platform/ios/CCDevice.mm | 26 +++++++++++-------- .../project.pbxproj.REMOVED.git-id | 2 +- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/cocos2dx/platform/ios/CCDevice.mm b/cocos2dx/platform/ios/CCDevice.mm index 6ced352414..f678244da7 100644 --- a/cocos2dx/platform/ios/CCDevice.mm +++ b/cocos2dx/platform/ios/CCDevice.mm @@ -6,13 +6,13 @@ #import // Accelerometer -#import -#import -#import +#include +#import @interface CCAccelerometerDispatcher : NSObject { cocos2d::Acceleration *_acceleration; + CMMotionManager *_motionManager; } + (id) sharedAccelerometerDispather; @@ -38,6 +38,7 @@ static CCAccelerometerDispatcher* s_pAccelerometerDispatcher; - (id) init { _acceleration = new cocos2d::Acceleration(); + _motionManager = [[CMMotionManager alloc] init]; return self; } @@ -45,6 +46,7 @@ static CCAccelerometerDispatcher* s_pAccelerometerDispatcher; { s_pAccelerometerDispatcher = nullptr; delete _acceleration; + [_motionManager release]; [super dealloc]; } @@ -52,25 +54,27 @@ static CCAccelerometerDispatcher* s_pAccelerometerDispatcher; { if (isEnabled) { - [[UIAccelerometer sharedAccelerometer] setDelegate:self]; + [_motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) { + [self accelerometer:accelerometerData]; + }]; } else { - [[UIAccelerometer sharedAccelerometer] setDelegate:nil]; + [_motionManager stopAccelerometerUpdates]; } } -(void) setAccelerometerInterval:(float)interval { - [[UIAccelerometer sharedAccelerometer] setUpdateInterval:interval]; + _motionManager.accelerometerUpdateInterval = interval; } -- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration +- (void)accelerometer:(CMAccelerometerData *)accelerometerData { - _acceleration->x = acceleration.x; - _acceleration->y = acceleration.y; - _acceleration->z = acceleration.z; - _acceleration->timestamp = acceleration.timestamp; + _acceleration->x = accelerometerData.acceleration.x; + _acceleration->y = accelerometerData.acceleration.y; + _acceleration->z = accelerometerData.acceleration.z; + _acceleration->timestamp = accelerometerData.timestamp; double tmp = _acceleration->x; diff --git a/samples/samples.xcodeproj/project.pbxproj.REMOVED.git-id b/samples/samples.xcodeproj/project.pbxproj.REMOVED.git-id index efbf8806ae..dc39ae6bd3 100644 --- a/samples/samples.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/samples/samples.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -b759f56a07f242b20e9c96f2a6cd49c4b5c9dc9b \ No newline at end of file +d846dfb9965fecfad1f6fe6190516035158c4d53 \ No newline at end of file