加速度计是一种能够感应设备一个方向上线性加速度的传感器.iOS设备中目前采用的是三轴加速度计,可以感应设备上X,Y,Z轴方向上线性加速度的变化.
访问iOS设备加速度的API有两个不同的阶段,以iOS5为分水岭,在iOS5之前主要使用UIAccelerometer和UIAccelerometerDelegate,而在iOS5之后使用CoreMotion框架中的CoreMotionManager等类.CoreMotion框架不仅可以获得设备的加速度,还可以获得设备的角速度(由陀螺仪传感器感应)
1.UIAccelerometer
由于UIAccelerometer 在iOS5以后就被封住了,所以就简单的了解了一下,需要注意的就是
<span style="font-size:18px;">[[UIAccelerometer sharedAccelerometer]setUpdateInterval:0.1]; [[UIAccelerometer sharedAccelerometer]setDelegate:self]; - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. [[UIAccelerometer sharedAccelerometer]setDelegate:nil]; }</span>
在代理方法中
<span style="font-size:18px;">- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration //acceleration 参数具有X,Y,Z的值,可以拿来使用 很简单</span>
添加CoreMotion.frameWork
<span style="font-size:18px;">- (void)viewDidLoad { [super viewDidLoad]; self.motionManager = [[CMMotionManager alloc]init];//注意CMMotionManager不是单例 self.motionManager.accelerometerUpdateInterval = 0.1;//设置读取时间间隔 if ([self.motionManager isAccelerometerAvailable]) {//判断是否可以使用加速度计 //获取主线程并发队列,在主线程里跟新UI [self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData *accelerometerData,NSError *error){ if (error) { [self.motionManager stopAccelerometerUpdates];//停止使用加速度计 }else{ self.XL.text = [NSString stringWithFormat:@"%f",accelerometerData.acceleration.x]; self.XP.progress = ABS(accelerometerData.acceleration.x); self.YL.text = [NSString stringWithFormat:@"%f",accelerometerData.acceleration.y]; self.YP.progress = ABS(accelerometerData.acceleration.y); self.ZL.text = [NSString stringWithFormat:@"%f",accelerometerData.acceleration.z]; self.ZP.progress = ABS(accelerometerData.acceleration.z); } }]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. [self.motionManager stopAccelerometerUpdates]; } </span>
3.感知设备方向
主要就是注意几点
1.在viewWillAppear中开启监听设备方向
- (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [[UIDevice currentDevice]beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receivedRotation:) name:UIDeviceOrientationDidChangeNotification object:nil]; }
2.在viewWillDisappear中关闭监听设备方向
- (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [[NSNotificationCenter defaultCenter]removeObserver:self]; [[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications]; }
UIDevice *device = [UIDevice currentDevice];//获取当前设备 //通过枚举判断方向 typedef NS_ENUM(NSInteger, UIDeviceOrientation) { UIDeviceOrientationUnknown, UIDeviceOrientationPortrait, // Device oriented vertically, home button on the bottom UIDeviceOrientationPortraitUpsideDown, // Device oriented vertically, home button on the top UIDeviceOrientationLandscapeLeft, // Device oriented horizontally, home button on the right UIDeviceOrientationLandscapeRight, // Device oriented horizontally, home button on the left UIDeviceOrientationFaceUp, // Device oriented flat, face up UIDeviceOrientationFaceDown // Device oriented flat, face down };
另外,转一篇UIProgressView的文章,可以参考
UIProgressView和UIActivityIndicator有些类似
原文:http://blog.sina.com.cn/s/blog_5aeb9f7b0101dv7x.html
更多干货,请支持原作:http://item.jd.com/11436547.html