iOS新加速計事件(陀螺儀和加速計)

來源:互聯網
上載者:User

標籤:

iOS新加速計事件

iOS新加速計事件

 1、iOS5.0以前,可以使用UIAcceleration來監聽加速計事件。

 2、Bug iOS5.0以後,UIAccelerometerDelegate已經被depreacated,如下:

  

   deprecated不是說不能說了,而是意味著在將來版本會刪除,所以如果不想更新知識的話,就使用UIAccelerometer吧。更保險的方法是使用一個Timer來檢查UIAcceleration,即不依賴於此Delegate回調。

 3、針對iOS4.0以上版本,推薦使用CMMotionManager來監聽加速計事件。涉及到下面幾個方法:

  

  

 4、其實,CMMotionManager也挺簡單的,加速計的方法就這麼幾個。

陀螺儀(感應器)和加速計:

加速計和陀螺儀的值都是通過Core Motion架構訪問的,此架構提供了CMMotionManager類。該類提供的所有資料都是用來描述使用者如何行動裝置的。

應用程式建立一個CMMotionManager執行個體,然後通過以下某種模式使用它:

1它可以在動作發生時執行一些代碼

2 它可以時刻監視一個持續更新的結構,使你隨時能夠訪問最新的值。

陀螺儀(感應器)和加速計的使用:

label屬性偵測器的Lines改為0,高度增加即可。

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak,nonatomic) IBOutlet UILabel *accelerometerLabel;
@property (weak,nonatomic) IBOutlet UILabel *gyroscopeLabel;

@end

.m檔案:

#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>

@interface ViewController ()

@property (strong,nonatomic) CMMotionManager *motionManager;
//@property (strong,nonatomic) NSOperationQueue *queue;//第一種
@property (strong,nonatomic) NSTimer *updateTime;

@end

@implementation ViewController

-(NSUInteger)supportedInterfaceOrientations{

    return UIInterfaceOrientationMaskPortrait;
}
-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    
    [self.motionManager startAccelerometerUpdates];
    [self.motionManager startGyroUpdates];
    
    self.updateTime = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0 target:self selector:@selector(updateDisplay) userInfo:nil repeats:YES];
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    [self.motionManager stopAccelerometerUpdates];
    [self.motionManager stopGyroUpdates];
    [self.updateTime invalidate];
    self.updateTime = nil;
}
-(void)updateDisplay{
    if (self.motionManager.accelerometerAvailable) {
        
        CMAccelerometerData *accelerometerData = self.motionManager.accelerometerData;
        self.accelerometerLabel.text =[NSString stringWithFormat:@"Accelerometer\n---\n""x: %+.2f\ny: %+.2f\nz: %+.2f",accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z];
    }
    
    if (self.motionManager.gyroAvailable) {
        
        CMGyroData *gyroData = self.motionManager.gyroData;
        self.gyroscopeLabel.text = [NSString stringWithFormat:@"Gyroscope\n---\n""x: %+.2f\ny: %+.2f\nz: %+.2f",gyroData.rotationRate.x,gyroData.rotationRate.y,gyroData.rotationRate.z];
    }

}
-(void)viewDidLoad{
    [super viewDidLoad];
    
    self.motionManager = [[CMMotionManager alloc]init];//動作管理器初始化
    
    if (self.motionManager.accelerometerAvailable) {
         self.motionManager.accelerometerUpdateInterval = 1.0/10.0; //0.1s更新一次
    }else{
     self.accelerometerLabel.text = @"This device has no accelerometer.";
    
    }
    
    if (self.motionManager.gyroAvailable) {
         self.motionManager.gyroUpdateInterval = 1.0/10.0;
    }else{
     self.accelerometerLabel.text = @"This device has no Gyroscope.";
    }
    
    
    /*
    self.queue = [[NSOperationQueue alloc]init];
    
    if (self.motionManager.isAccelerometerAvailable) {//判斷有沒有加速計
        
        self.motionManager.accelerometerUpdateInterval = 1.0/10.0; //0.1s更新一次
        //告訴動作管理器開始報告加速計更新
        [self.motionManager startAccelerometerUpdatesToQueue:self.queue withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
            
            NSString *labelText;
            
            if (error) {
                
                [self.motionManager stopAccelerometerUpdates];
                labelText = [NSString stringWithFormat:@"Accelerometer encounted error:%@",error];
            }else{
            
                labelText = [NSString stringWithFormat:@"Accelerometer\n---\n""x: %+.2f\ny: %+.2f\nz: %+.2f",accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z];
            }
            
            dispatch_async(dispatch_get_main_queue(),^{
            
                self.accelerometerLabel.text = labelText;
            });
            
        }];
    }else {
    self.accelerometerLabel.text = @"This device has no accelerometer.";
      
    }
    
    
    
    if (self.motionManager.gyroAvailable) {//判斷有沒有陀螺儀
        
        self.motionManager.gyroUpdateInterval = 1.0/10.0;
        [self.motionManager startGyroUpdatesToQueue:self.queue withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {
            
            NSString *labelText;
            
            if (error) {
                [self.motionManager stopGyroUpdates];
                labelText = [NSString stringWithFormat:@"Gyroscope encounted error:%@",error];
            }else{
                 labelText = [NSString stringWithFormat:@"Gyroscope\n---\n""x: %+.2f\ny: %+.2f\nz: %+.2f",gyroData.rotationRate.x,gyroData.rotationRate.y,gyroData.rotationRate.z];
              
            }
            
            dispatch_async(dispatch_get_main_queue(),^{
                
                self.gyroscopeLabel.text = labelText;
            });
        }];
    }else{
         self.accelerometerLabel.text = @"This device has no Gyroscope.";
    }
     */

}
@end

iOS新加速計事件(陀螺儀和加速計)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.