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>
// Accelerometer
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <functional>
#include<CoreMotion/CoreMotion.h>
#import<CoreFoundation/CoreFoundation.h>
@interface CCAccelerometerDispatcher : NSObject<UIAccelerometerDelegate>
{
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;

View File

@ -1 +1 @@
b759f56a07f242b20e9c96f2a6cd49c4b5c9dc9b
d846dfb9965fecfad1f6fe6190516035158c4d53