21.0~21.5 加速器与陀螺仪 Core Motion

来源:互联网 发布:mod jk 源码编译 编辑:程序博客网 时间:2024/04/30 21:21

21.0. IntroductionCore Motion

在使用加速器和陀螺仪之前,你应该先判断设备是否可用。

加速器可以获得在x,y,z轴上的移动数据,陀螺仪可以知到这些方向上与地面的关系

the developers need to know not only whether the device is moving on the x-,y-, andz-axes (information they can get from the accelerometer), but also whether it is changing in relation to the Earth along these directions, which requires a gyroscope.

使用时需要引入Core Motion framework

模拟器上没有加速器和陀螺仪。只能使用Hardware->Shake Gesture

21.1. Detecting the Availability of an Accelerometer

检测加速器

-(void)testAccelerometerAvailable

{

    CMMotionManager *motionManager = [[CMMotionManageralloc]init];

   if ([motionManagerisAccelerometerAvailable]){

        NSLog(@"Accelerometer is available.");

    }else{

        NSLog(@"Accelerometer is not available.");

    }

   if ([motionManagerisAccelerometerActive]){

        NSLog(@"Accelerometer is active.");

    }else {

        NSLog(@"Accelerometer is not active.");

    }

}

模拟器:

2014-08-07 16:51:01.358 cookbook21[662:a0b] Accelerometer is not available.

2014-08-07 16:51:01.359 cookbook21[662:a0b] Accelerometer is not active.

真机:

2014-08-07 17:13:54.686 cookbook21[136:907] Accelerometer is available.

2014-08-07 17:13:54.688 cookbook21[136:907] Accelerometer is not active.


21.2. Detecting the Availability of a Gyroscope

检测陀螺仪


-(void)testGyroscrope

{

    CMMotionManager *motionManager = [[CMMotionManageralloc]init];

   if ([motionManagerisGyroAvailable]){

        NSLog(@"Gryro is available.");

    }else {

        NSLog(@"Gyro is not available.");

    }

   if ([motionManagerisGyroActive]){

        NSLog(@"Gryo is active.");

    }else {

        NSLog(@"Gryo is not active.");

    }

}

模拟器:

2014-08-07 16:59:22.412 cookbook21[690:a0b] Gyro is not available.

2014-08-07 16:59:22.412 cookbook21[690:a0b] Gryo is not active.

真机:

2014-08-07 17:14:28.801 cookbook21[151:907] Gryro is available.

2014-08-07 17:14:28.803 cookbook21[151:907] Gryo is not active.



21.3. Retrieving Accelerometer Data

让iOS发加速器的数据给应用

iOS发送加速器的三维数据给应用,其封装在数据结构里

typedef struct {doublex;doubley;doublez; }CMAcceleration;

x轴:设备的水平中心从左至右,值范围-1~+1

y轴:设备的垂直中心从下至上,值范围-1~+1

z轴:设备由后至前,值范围-1~+1

不懂怎么翻译,下面是原文:

  • The x-axis runs from left to right at the horizontal center of the device,with values ranging from1 to +1 from left to right. 
  • They-axis runs from bottom to top at the vertical center of the device,with values ranging from1 to +1 from bottom to top. 
  • The z-axis runs from the back of the device, through the device toward you, with values ranging from1 to +1 from back to front. 

通过后面的实验,我的理解是,有这么个三维空间,x轴正方向为向右方向,y轴正方向为向下方向,z轴正方向为指向自己的方向。

实验一:绕着Z轴旋转

1,手机垂直向上(top point to the sky),这时(x:0.0,y:-1.0,z:0.0)

2,顺时针旋转90度,这时(x:+1.0,y:0.0,z:0.0)

3,再顺时针旋转90度(top point to the ground),这时(x:0.0,y:1.0,z:0.0)

4,再顺时针旋转90度,这时(x:-1.0,y:0.0,z:0.0)

5,再顺时针旋转90度(top point to the sky),这时(x:0.0,y:-1.0,z:0.0)

实验二:绕着X轴旋转

1,手机垂直向上(top point to the sky),这时(x:0.0,y:-1.0,z:0.0)

2,从前往后旋转90度,这时(x:0.0,y:0.0,z:-1.0)

3,再旋转90度(top point to the ground),这时(x:0.0,y:1.0,z:0.0)

4,再旋转90度,这时(x:0.0,y:0.0,z:1.0)

5,再旋转90度(top point to the sky),这时(x:0.0,y:-1.0,z:0.0)

实验三:绕着Y轴旋转

额。。。这个貌似比较难,书上鼓励我们自己动手。我就自以为的这么做了:

1,让手机面向我们趴倒在桌上,这时(x:0.0,y:0.0,z:1.0)

2,让手机右侧躺着(landscape left),这时(x:1.0,y:0.0,z:0.0)

3,让手机向后躺在桌上,这时(x:0.0,y:0.0,z:-1.0)

4,让手机左侧躺着(landscape left),这时(x:-1.0,y:0.0,z:0.0)

5,回到第一步了

-(void)testAccelerometerUpdate

{

   self.motionManager = [[CMMotionManageralloc]init];

    if ([self.motionManagerisAccelerometerAvailable]){

        NSOperationQueue *queue = [[NSOperationQueuealloc]init];

        [self.motionManager

         startAccelerometerUpdatesToQueue:queue

        withHandler:^(CMAccelerometerData *accelerometerData,NSError *error) {

             NSLog(@"X = %.04f, Y = %.04f, Z = %.04f",

                   accelerometerData.acceleration.x,

                   accelerometerData.acceleration.y,

                   accelerometerData.acceleration.z);

         }];

        

    }else {

        NSLog(@"Accelerometer is not available.");

    }

    

}

打印(竖直的时候):

2014-08-08 11:31:37.761 cookbook21[273:1603] X = 0.0906, Y = -0.9679, Z = -0.1973

2014-08-08 11:31:37.761 cookbook21[273:4707] X = 0.0906, Y = -0.9727, Z = -0.2069

2014-08-08 11:31:37.770 cookbook21[273:1103] X = 0.0896, Y = -0.9679, Z = -0.1906

2014-08-08 11:31:37.777 cookbook21[273:1103] X = 0.0885, Y = -0.9660, Z = -0.1801

2014-08-08 11:31:37.858 cookbook21[273:1603] X = 0.0985, Y = -0.9658, Z = -0.1828

2014-08-08 11:31:37.861 cookbook21[273:1603] X = 0.0982, Y = -0.9840, Z = -0.1915

2014-08-08 11:31:37.862 cookbook21[273:1103] X = 0.1094, Y = -0.9791, Z = -0.2163

2014-08-08 11:31:37.862 cookbook21[273:1603] X = 0.1122, Y = -0.9838, Z = -0.2057

。。。。。。



21.4. Detecting Shakes on an iOS Device

摇一摇

- (BOOL)canBecomeFirstResponder

{

    returnYES;// default is NO

}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event

{

    if (motion ==UIEventSubtypeMotionShake){

        NSLog(@"%@  %s",NSStringFromClass([selfclass]),__FUNCTION__);

    }

    

}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{

    if (motion ==UIEventSubtypeMotionShake){

        NSLog(@"%@  %s",NSStringFromClass([self class]),__FUNCTION__);

    }

}


- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{

    if (motion ==UIEventSubtypeMotionShake){

        NSLog(@"%@  %s",NSStringFromClass([self class]),__FUNCTION__);

    }

}


打印:

2014-08-08 11:54:26.162 cookbook21[398:907] ViewController  -[ViewController motionBegan:withEvent:]

2014-08-08 11:54:28.526 cookbook21[398:907] ViewController  -[ViewController motionEnded:withEvent:]

2014-08-08 11:54:30.897 cookbook21[398:907] ViewController  -[ViewController motionBegan:withEvent:]

2014-08-08 11:54:31.520 cookbook21[398:907] ViewController  -[ViewController motionEnded:withEvent:]

2014-08-08 11:54:33.277 cookbook21[398:907] ViewController  -[ViewController motionBegan:withEvent:]

2014-08-08 11:54:34.976 cookbook21[398:907] ViewController  -[ViewController motionCancelled:withEvent:]

2014-08-08 11:54:41.767 cookbook21[398:907] ViewController  -[ViewController motionBegan:withEvent:]

2014-08-08 11:54:49.834 cookbook21[398:907] ViewController  -[ViewController motionEnded:withEvent:]

2014-08-08 11:54:51.071 cookbook21[398:907] ViewController  -[ViewController motionBegan:withEvent:]

2014-08-08 11:54:51.681 cookbook21[398:907] ViewController  -[ViewController motionEnded:withEvent:]

2014-08-08 11:54:57.766 cookbook21[398:907] ViewController  -[ViewController motionBegan:withEvent:]

2014-08-08 11:55:02.295 cookbook21[398:907] ViewController  -[ViewController motionEnded:withEvent:]

2014-08-08 11:55:03.735 cookbook21[398:907] ViewController  -[ViewController motionBegan:withEvent:]

2014-08-08 11:55:07.353 cookbook21[398:907] ViewController  -[ViewController motionEnded:withEvent:]

2014-08-08 11:55:08.156 cookbook21[398:907] ViewController  -[ViewController motionBegan:withEvent:]

2014-08-08 11:55:08.795 cookbook21[398:907] ViewController  -[ViewController motionEnded:withEvent:]

2014-08-08 11:55:12.677 cookbook21[398:907] ViewController  -[ViewController motionBegan:withEvent:]

2014-08-08 11:55:18.639 cookbook21[398:907] ViewController  -[ViewController motionCancelled:withEvent:]



21.5. Retrieving Gyroscope Data

-(void)testRetrieveGyroscopeData

{

    CMMotionManager *manager = [[CMMotionManageralloc]init];

   if ([managerisGyroAvailable]){

       if ([managerisGyroActive] ==NO){

            [managersetGyroUpdateInterval:1.0f /40.0f];

            NSOperationQueue *queue = [[NSOperationQueuealloc]init];

            NSLog(@"startGyroUpdatesToQueue");

            [manager

             startGyroUpdatesToQueue:queue

            withHandler:^(CMGyroData *gyroData,NSError *error) {

                NSLog(@"Gyro Rotation x = %.04f", gyroData.rotationRate.x);

                NSLog(@"Gyro Rotation y = %.04f", gyroData.rotationRate.y);

                NSLog(@"Gyro Rotation z = %.04f", gyroData.rotationRate.z);

             }];

        }else {

            NSLog(@"Gyro is already active.");

        }

    }else {

        NSLog(@"Gyro isn't available.");

    }

}

打印:

2014-08-12 14:54:20.713 cookbook21[828:907] startGyroUpdatesToQueue


很失败,愣是收不到数据

0 0