ios中加速器和重力感应器 ios重力感应游戏

首先导入CoreMotion框架,新建两个label
#import

@interface ViewController : UIViewController{

CMMotionManager *_manager;
NSTimer*_timer;
}
//动作管理器指针
@property(nonatomic,retain)CMMotionManager *manager;
@property (retain, nonatomic) IBOutlet UILabel*accelerometerLabel;
@property (retain, nonatomic) IBOutlet UILabel*gyroscopeLabel;
@property(nonatomic,retain)NSTimer *timer;

@end
//
self.manager = [[CMMotionManager alloc]init];
//添加一个队列线程
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
//判断是否可以加速
if(_manager.accelerometerAvailable == YES) {
//更新速度
_manager.accelerometerUpdateInterval = 0.1;
//block
[_manager startAccelerometerUpdatesToQueue:queuewithHandler:^(CMAccelerometerData *accelerometerData, NSError*error) {
NSString *acceleterString;
if (error) {
[_manager stopAccelerometerUpdates];
acceleterString = [NSStringstringWithFormat:@"acceletererror:%@",error];
}else{
//重力加速度三维分量
acceleterString = [NSStringstringWithFormat:@"%fn,%fn,%fn",accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z];
//跳转到主线程
[self.accelerometerLabelperformSelectorOnMainThread:@selector(setText:)withObject:acceleterString waitUntilDone:NO];
}
}];
}else{
self.accelerometerLabel.text = @"设备没有加速器";
}
//重力感应
if(_manager.gyroAvailable == YES) {
_manager.gyroUpdateInterval = 1.0;
[_manager startGyroUpdatesToQueue:queue withHandler:^(CMGyroData*gyroData, NSError *error) {
NSString *gryString;
if (error) {
//停止重力感应更新
[_manager stopGyroUpdates];
gryString = [NSString stringWithFormat:@"gryerror:%@",error];
}else{
gryString = [NSStringstringWithFormat:@"%fn,%fn,%fn",gyroData.rotationRate.x,gyroData.rotationRate.y,gyroData.rotationRate.z];
[self.gyroscopeLabelperformSelectorOnMainThread:@selector(setText:)withObject:gryString waitUntilDone:NO];
}
}];
}



//方法2
self.manager = [[CMMotionManager alloc] init];
if(_manager.accelerometerAvailable == YES) {
_manager.accelerometerUpdateInterval = 1.0;
[_manager startAccelerometerUpdates];
}else{
self.accelerometerLabel.text = @"设备不支持加速计";
}

if(_manager.gyroAvailable == YES) {
ios中加速器和重力感应器 ios重力感应游戏
_manager.gyroUpdateInterval = 2.0;
[_manager startGyroUpdates];
}else{
self.gyroscopeLabel.text = @"设备不支持重力感应";
}
//在viewWillAppear里设定一个定时器用于更新

- (void)viewWillAppear:(BOOL)animated{
[superviewWillAppear:animated];
self.timer =[NSTimer scheduledTimerWithTimeInterval:1.0f target:selfselector:@selector(updateDisPlay) userInfo:nil repeats:YES];
}

- (void)updateDisPlay{
if(_manager.accelerometerAvailable == YES) {
CMAccelerometerData *accelerometerData =_manager.accelerometerData;
//重力加速度三维分量
self.accelerometerLabel.text = [NSStringstringWithFormat:@"%fn,%fn,%fn",accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z];
}
//是否有重力感应
if(_manager.gyroAvailable == YES) {
CMGyroData *gyroData = _manager.gyroData;
self.gyroscopeLabel.text = [NSStringstringWithFormat:@"%fn,%fn,%fn",gyroData.rotationRate.x,gyroData.rotationRate.y,gyroData.rotationRate.z];
}
}

  

爱华网本文地址 » http://www.aihuau.com/a/25101017/334388.html

更多阅读

教你如何修改删除淘宝中评和差评 淘宝差评修改

教你如何修改删除淘宝中评和差评——简介很多时候,买家在给中评的时候,特别是新手买家,他们在给中评的时候,以为不扣分,宝贝确实一般,觉得中评应该没什么,但是他们不知道一个中评,对一个卖家来说有多重要,虽然中评不扣分,但是会直接的影响到卖

声明:《ios中加速器和重力感应器 ios重力感应游戏》为网友我要当学霸分享!如侵犯到您的合法权益请联系我们删除