首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

ios陀螺仪和加快计

2013-08-11 
ios陀螺仪和加速计????? ios设备中有的加速计可以测量出加速度和重力。陀螺仪可用于确定设备的方向与每条坐

ios陀螺仪和加速计

????? ios设备中有的加速计可以测量出加速度和重力。陀螺仪可用于确定设备的方向与每条坐标轴之间的夹角,可用于读取描述设备围绕其轴的旋转的值。

添加CoreMotion.framework.

以下为例子代码:

////  ViewController.h//  MotionTest//  Created by Dwen on 13-2-26.//  Copyright (c) 2013年 Dwen. All rights reserved.//#import <UIKit/UIKit.h>#import <CoreMotion/CoreMotion.h>@interface ViewController : UIViewController@property (strong, nonatomic) CMMotionManager *motionManager;@property (weak, nonatomic) IBOutlet UILabel *accelerometerLabel;@property (weak, nonatomic) IBOutlet UILabel *gyroscopeLabel;@end

?

////  ViewController.m//  MotionTest////  Created by Dwen on 13-2-26.//  Copyright (c) 2013年 Dwen. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController@synthesize motionManager;@synthesize accelerometerLabel;@synthesize gyroscopeLabel;- (void)viewDidLoad{    [super viewDidLoad];self.motionManager = [[CMMotionManager alloc] init];    NSOperationQueue *queue = [[NSOperationQueue alloc] init];    //加速计    if (motionManager.accelerometerAvailable) {        motionManager.accelerometerUpdateInterval = 1.0/10.0;        [motionManager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData *accelerometerData,NSError *error){            NSString *labelText;            if (error) {                [motionManager stopAccelerometerUpdates];                labelText = [NSString stringWithFormat:@"Accelerometer encountered error: %@",error];            }else{                labelText = [NSString stringWithFormat:@"加速计\nx: %+.2f\ny: %+.2f\nz: %+.2f",accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z];            }            [accelerometerLabel performSelectorOnMainThread:@selector(setText:) withObject:labelText waitUntilDone:NO];        }];    }else{        accelerometerLabel.text = @"This device has no accelerometer.";    }    //陀螺仪    if (motionManager.gyroAvailable) {        motionManager.gyroUpdateInterval = 1.0/10.0;        [motionManager startGyroUpdatesToQueue:queue withHandler:^(CMGyroData *gyroData,NSError *error){            NSString *labelText;            if (error) {                [motionManager stopGyroUpdates];                labelText = [NSString stringWithFormat:@"Gyroscope encountered error: %@",error];            }else{                labelText = [NSString stringWithFormat:@"陀螺仪\nx: %+.2f\ny: %+.2f\nz: %+.2f",gyroData.rotationRate.x,gyroData.rotationRate.y,gyroData.rotationRate.z];            }            [gyroscopeLabel performSelectorOnMainThread:@selector(setText:) withObject:labelText waitUntilDone:NO];        }];    }else{        gyroscopeLabel.text = @"This device has no gyroscope";    }}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];}@end

?

热点排行