mirror of https://github.com/axmolengine/axmol.git
issues #2677:Use CoreMotion's API to replace UIAccelerometer.
This commit is contained in:
parent
97315900b0
commit
5acac436f9
|
@ -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;
|
||||||
|
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
b759f56a07f242b20e9c96f2a6cd49c4b5c9dc9b
|
d846dfb9965fecfad1f6fe6190516035158c4d53
|
Loading…
Reference in New Issue