读书笔记---加速度计

来源:互联网 发布:mac os 优化 编辑:程序博客网 时间:2024/06/05 22:37

加速度计是一种能够感应设备一个方向上线性加速度的传感器.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>

2.CoreMotion

添加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];}

3.
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有些类似


但是不同之处在于,
UIProgressView能够更加精确的反应进度
UIActivityIndicator则只能表示事物在进行中

有一个例子是在Mail程序中当在下载信息的时候,有一个UIProgressView显示在底部

初始化方法
- (id)initWithProgressViewStyle:(UIProgressViewStyle)style

UIProgressViewStyle 有两个枚举变量
1. UIProgressViewStyleDefault         
UIProgressView的使用

2. UIProgressViewStyleBar 一般用于toolbar
UIProgressView的使用

配置UIProgressView
progressViewStyle 属性
progressTintColor 属性 填充部分的颜色(譬如进度到30%,那么30%部分的颜色就是这个属性)iOS 5新增
progressImage 属性,填充部分的图片,设置图片之后progressTintColor无效,iOS 5新增
trackTintColor 属性,未填充部分的颜色 iOS 5新增
trackImage 属性,未填充部分的图片,设置图片后trackTintColor无效  iOS 5新增

管理UIProgressView
progress 属性 当前进度值
- (void)setProgress:(float)progress animated:(BOOL)animated
设置当前进度值 iOS 5新增

原文:http://blog.sina.com.cn/s/blog_5aeb9f7b0101dv7x.html


更多干货,请支持原作:http://item.jd.com/11436547.html

0 0