issues #2677:Use CoreMotion's API to replace UIAccelerometer.

This commit is contained in:
Dhilan007 2013-10-08 10:11:52 +08:00
parent 97315900b0
commit 5acac436f9
2 changed files with 16 additions and 12 deletions

View File

@ -6,13 +6,13 @@
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
// Accelerometer // Accelerometer
#import <Foundation/Foundation.h> #include<CoreMotion/CoreMotion.h>
#import <UIKit/UIKit.h> #import<CoreFoundation/CoreFoundation.h>
#import <functional>
@interface CCAccelerometerDispatcher : NSObject<UIAccelerometerDelegate> @interface CCAccelerometerDispatcher : NSObject<UIAccelerometerDelegate>
{ {
cocos2d::Acceleration *_acceleration; cocos2d::Acceleration *_acceleration;
CMMotionManager *_motionManager;
} }
+ (id) sharedAccelerometerDispather; + (id) sharedAccelerometerDispather;
@ -38,6 +38,7 @@ static CCAccelerometerDispatcher* s_pAccelerometerDispatcher;
- (id) init - (id) init
{ {
_acceleration = new cocos2d::Acceleration(); _acceleration = new cocos2d::Acceleration();
_motionManager = [[CMMotionManager alloc] init];
return self; return self;
} }
@ -45,6 +46,7 @@ static CCAccelerometerDispatcher* s_pAccelerometerDispatcher;
{ {
s_pAccelerometerDispatcher = nullptr; s_pAccelerometerDispatcher = nullptr;
delete _acceleration; delete _acceleration;
[_motionManager release];
[super dealloc]; [super dealloc];
} }
@ -52,25 +54,27 @@ static CCAccelerometerDispatcher* s_pAccelerometerDispatcher;
{ {
if (isEnabled) if (isEnabled)
{ {
[[UIAccelerometer sharedAccelerometer] setDelegate:self]; [_motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
[self accelerometer:accelerometerData];
}];
} }
else else
{ {
[[UIAccelerometer sharedAccelerometer] setDelegate:nil]; [_motionManager stopAccelerometerUpdates];
} }
} }
-(void) setAccelerometerInterval:(float)interval -(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->x = accelerometerData.acceleration.x;
_acceleration->y = acceleration.y; _acceleration->y = accelerometerData.acceleration.y;
_acceleration->z = acceleration.z; _acceleration->z = accelerometerData.acceleration.z;
_acceleration->timestamp = acceleration.timestamp; _acceleration->timestamp = accelerometerData.timestamp;
double tmp = _acceleration->x; double tmp = _acceleration->x;

View File

@ -1 +1 @@
b759f56a07f242b20e9c96f2a6cd49c4b5c9dc9b d846dfb9965fecfad1f6fe6190516035158c4d53