标签:anim 完整 cto update imp nta style tde bool
加速度通过CMMotionManager来获取
(一)头文件
#import "AccViewController.h" #import <CoreMotion/CoreMotion.h> @interface AccViewController ()<UIAccelerometerDelegate> { NSTimer *updateTimer; } @property(nonatomic,strong) CMMotionManager *motionManager; @end
(二)判断是否可用
_motionManager=[[CMMotionManager alloc] init]; if(_motionManager.deviceMotionAvailable) { [_motionManager startDeviceMotionUpdates]; }else { NSLog(@"Motion unavailable"); }
(三)获取当前运动状态
-(void) updateAcc { NSLog(@"Update acc"); if(_motionManager.deviceMotionAvailable) { CMDeviceMotion *motion=_motionManager.deviceMotion; accXvalue.text=[NSString stringWithFormat:@"%f",motion.rotationRate.x]; accYvalue.text=[NSString stringWithFormat:@"%f",motion.rotationRate.y]; accZvalue.text=[NSString stringWithFormat:@"%f",motion.rotationRate.z]; } }
可以通过CMDeviceMotion 获取当前手机的各种运动状态,不止上述中的三种。
(四)通过定时器实时更新
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; //使用定时器周期性获取设备移动信息 updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateAcc) userInfo:nil repeats:YES]; [updateTimer fire]; [updateTimer fire]; }
?//----------------------------------------------------完整代码/----------------------------------------------------//
#import "AccViewController.h" #import <CoreMotion/CoreMotion.h> @interface AccViewController ()<UIAccelerometerDelegate> { NSTimer *updateTimer; } @property(nonatomic,strong) CMMotionManager *motionManager; @end UILabel *accXvalue; UILabel *accYvalue; UILabel *accZvalue; @implementation AccViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor=[UIColor greenColor]; UILabel *accX=[[UILabel alloc] init]; accX.text=@"acc_X:"; accX.frame=CGRectMake(100, 200, 100, 15); [self.view addSubview:accX]; accXvalue=[[UILabel alloc] init]; accXvalue.text=@""; accXvalue.frame=CGRectMake(210, 200, 150, 15); [self.view addSubview:accXvalue]; UILabel *accY=[[UILabel alloc] init]; accY.text=@"acc_Y:"; accY.frame=CGRectMake(100, 300, 100, 15); [self.view addSubview:accY]; accYvalue=[[UILabel alloc] init]; accYvalue.text=@""; accYvalue.frame=CGRectMake(210, 300, 150, 15); [self.view addSubview:accYvalue]; UILabel *accZ=[[UILabel alloc] init]; accZ.text=@"acc_Z:"; accZ.frame=CGRectMake(100, 400, 100, 15); [self.view addSubview:accZ]; accZvalue=[[UILabel alloc] init]; accZvalue.text=@""; accZvalue.frame=CGRectMake(210, 400, 150, 15); [self.view addSubview:accZvalue]; _motionManager=[[CMMotionManager alloc] init]; if(_motionManager.deviceMotionAvailable) { [_motionManager startDeviceMotionUpdates]; }else { NSLog(@"Motion unavailable"); } } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; //使用定时器周期性获取设备移动信息 updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateAcc) userInfo:nil repeats:YES]; [updateTimer fire]; [updateTimer fire]; } -(void) updateAcc { NSLog(@"Update acc"); if(_motionManager.deviceMotionAvailable) { CMDeviceMotion *motion=_motionManager.deviceMotion; accXvalue.text=[NSString stringWithFormat:@"%f",motion.rotationRate.x]; accYvalue.text=[NSString stringWithFormat:@"%f",motion.rotationRate.y]; accZvalue.text=[NSString stringWithFormat:@"%f",motion.rotationRate.z]; } }
标签:anim 完整 cto update imp nta style tde bool
原文地址:https://www.cnblogs.com/llstart-new0201/p/9722055.html