你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iphone中加速器,陀螺儀,磁力計的使用和實現

iphone中加速器,陀螺儀,磁力計的使用和實現

編輯:IOS開發綜合

#import "RootViewController.h"

#import //需要在frameworks中引入CoreMotion.framework框架

@interface RootViewController ()

{

CMMotionManager *motionManager;

UIImageView* imageView;

}

@end


@implementation RootViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {

// Custom initialization

}

return self;

}


- (void)viewDidLoad

{

[super viewDidLoad];

imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"33.jpeg"]];

imageView.backgroundColor=[UIColor redColor];

imageView.frame = CGRectMake(0, 0, 50, 50);

imageView.center = self.view.center;

[self.view addSubview:imageView];

//Core Motion的使用就是一三部曲:初始化,獲取數據,處理後事。

//可以參考資料:http://www.kunli.info/2010/07/30/motion/

//在初始化階段,不管你要獲取的是什麼數據,首先需要做的就是

motionManager = [[CMMotionManager alloc] init];

//所有的操作都會由這個manager接管。後面的初始化操作相當直觀,以加速度的pull方式為例

[motionManager startAccelerometerUpdates]; // 開始更新,後台線程開始運行。這是pull方式。

if (!motionManager.accelerometerAvailable ) {

// fail code // 檢查傳感器到底在設備上是否可用

NSLog(@"傳感器不可用");

}else{

motionManager.accelerometerUpdateInterval = 1/60.0; // 告訴manager,更新頻率

//如果是push方式,更新的代碼可以寫成這樣

[motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData *latestAcc, NSError *error)

{

NSLog(@"執行block方法");

CMAccelerometerData *data = latestAcc;//motionManager.accelerometerData;

NSNumber *x = [NSNumber numberWithDouble:data.acceleration.x];

NSNumber *y = [NSNumber numberWithDouble:data.acceleration.y];

NSNumber *z = [NSNumber numberWithDouble:data.acceleration.z];

NSLog(@"x=%@------y=%@--------z=%@",x,y,z);

if (data.acceleration.x > 0) {

imageView.center = CGPointMake(imageView.center.x + 1, imageView.center.y);//向右

}

if(data.acceleration.x < 0){

imageView.center = CGPointMake(imageView.center.x - 1, imageView.center.y);//向左

}

if (data.acceleration.y > 0){

imageView.center = CGPointMake(imageView.center.x , imageView.center.y - 1);//向上

}

if (data.acceleration.y < 0){

imageView.center = CGPointMake(imageView.center.x , imageView.center.y + 1);//向下

}

NSLog(@"*****************************");

}];


}

UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];

button.frame=CGRectMake(60, 50, 150, 30);

[button setTitle:@"停止" forState:UIControlStateNormal];

[button addTarget:self action:@selector(didstop) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button];

UIButton *button1=[UIButton buttonWithType:UIButtonTypeRoundedRect];

button1.frame=CGRectMake(60, 450, 150, 30);

[button1 setTitle:@"獲取" forState:UIControlStateNormal];

[button1 addTarget:self action:@selector(didlook) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button1];

}

-(void)didlook

{

//要數據

CMAccelerometerData *data = motionManager.accelerometerData;

NSNumber *x = [NSNumber numberWithDouble:data.acceleration.x];

NSNumber *y = [NSNumber numberWithDouble:data.acceleration.y];

NSNumber *z = [NSNumber numberWithDouble:data.acceleration.z];

NSLog(@"x=%@------y=%@--------z=%@",x,y,z);


}




-(void)didstop

{

//最後是處理後事,就是在你不需要Core Motion進行處理的時候,釋放資源

[motionManager stopAccelerometerUpdates];


}

- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}


@end


  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved