加速度計是一種能夠感應裝置一個方向上線性加速度的傳感器.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
2. UIProgressViewStyleBar 一般用于toolbar
配置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