IOS 特定于设备的开发:Core Motion基础

摘要:
旋转包括x、y和z角速度值,每个单位表示地球提供的标准重力加速度(9.8 m/s平方)用户加速度:用户矢量和重力矢量表示提供给设备的总加速度_ motionManager=[[CMMotionManageralloc]init];

    Core Motion框架集中了运动数据处理。该框架是在IOS 4 SDK中引入的,用于取代accelerometer加速计访问。它提供了对3个关键的机载传感器的集中式监测。这些传感器有陀螺仪、磁力计和加速计组成,其中陀螺仪用于测量设备的旋转,磁力计提供了一种测量罗盘方位的方式,加速计用于监测沿着3根轴的重力变化。第四个入口点称为设备移动(device motion),他把全部3中传感器都结合进单个监测系统中。

   Core Motion使用来自这些传感器原始值创建可度的测量结果,主要表现为力向量的形式。可测量的项包括以下属性:

     》设备姿势(attitude):设备相对于某个参照画面的方向。姿势被表示为摇晃,前倾和左右摇摆的角度,他们都以弧度为单位。

     》旋转速率(rotationRate):设备围绕它的三根轴中的每一根轴旋转的速率。旋转包括x、y和z角速度值,他们以弧度/秒为单位。

     》重力(gravity):设备当前的加速度向量,由正常的重力场提供。重力的单位是g's,分别沿着x、y和z轴来测量。每个单位代表由地球提供的标准重力加速度(9.8米/秒的平方)

     》用户加速度(userAcceleration):用户提供的加度素向量。像重力一样,用户加速度的单位也是g's,分别沿着x、y和z轴来测量。当把它们加到一起时,用户向量和重力向量代表给设备提供的总加速度。

     》磁场(magneticField):代表设备临近区域的总磁场的向量。磁场是沿着x,y和z轴以为特斯拉(microtesla)为单位测量的。还提供了校准精度,通知应用程序有关磁场测量的质量。

1.测试传感器

你可以使用在Info.plist中设置使用或排除机载传感器,也可以在程序中来测试每种可能的Core Motion支持:

@property (nonatomic , strong)CMMotionManager *motionManager;


 _motionManager = [[CMMotionManager alloc]init];
    //监测陀螺仪
    if(_motionManager.gyroAvailable)
    {
        [_motionManager startGyroUpdates];
    }
    
    //监测磁力计
    if(_motionManager.magnetometerAvailable)
    {
        [_motionManager startMagnetometerUpdates];
    }
    
    //监测重力感应器
    if(_motionManager.accelerometerAvailable)
    {
        [_motionManager startAccelerometerUpdates];
    }
    
    //监测Device Motion
    if(_motionManager.deviceMotionAvailable)
    {
        [_motionManager startDeviceMotionUpdates];
    }

开始更新不会产生像使用UIAccelerometer时遇到的委托回调机制。作为替代,你将负责轮询每个值,或者可以使用基于块的更新机制,执行在每次更新时提供的一个块(例如,startAccelerometerUpdatesToQueue:withHandler:).

2.使用Core motion做加速计蝴蝶飞的程序如下

@implementation TestBedViewController
{
    UIImageView *butterfly;
    
    float xaccel;
    float xvelocity;
    float yaccel;
    float yvelocity;
    
    float mostRecentAngle;
    
    CMMotionManager *motionManager;
    NSTimer *timer;
}

- (void) tick
{
    butterfly.transform = CGAffineTransformIdentity;
    
    // Move the butterfly according to the current velocity vector
    CGRect rect = CGRectOffset(butterfly.frame, xvelocity, 0.0f);
    if (CGRectContainsRect(self.view.bounds, rect))
        butterfly.frame = rect;
    
    rect = CGRectOffset(butterfly.frame, 0.0f, yvelocity);
    if (CGRectContainsRect(self.view.bounds, rect))
        butterfly.frame = rect;
    
    butterfly.transform = CGAffineTransformMakeRotation(mostRecentAngle + M_PI_2);
}

- (void) shutDownMotionManager
{
    NSLog(@"Shutting down motion manager");
    [motionManager stopAccelerometerUpdates];
    motionManager = nil;
    
    [timer invalidate];
    timer = nil;
}

- (void) establishMotionManager
{
    if (motionManager)
        [self shutDownMotionManager];
    
    NSLog(@"Establishing motion manager");
    
    // Establish the motion manager
    motionManager = [[CMMotionManager alloc] init];
    if (motionManager.accelerometerAvailable)
        [motionManager
         startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc] init]
         withHandler:^(CMAccelerometerData *data, NSError *error)
         {
             // extract the acceleration components
             float xx = -data.acceleration.x;
             float yy = data.acceleration.y;
             mostRecentAngle = atan2(yy, xx);
             
             // Has the direction changed?
             float accelDirX = SIGN(xvelocity) * -1.0f;
             float newDirX = SIGN(xx);
             float accelDirY = SIGN(yvelocity) * -1.0f;
             float newDirY = SIGN(yy);
             
             // Accelerate. To increase viscosity lower the additive value
             if (accelDirX == newDirX) xaccel = (abs(xaccel) + 0.85f) * SIGN(xaccel);
             if (accelDirY == newDirY) yaccel = (abs(yaccel) + 0.85f) * SIGN(yaccel);
             
             // Apply acceleration changes to the current velocity
             xvelocity = -xaccel * xx;
             yvelocity = -yaccel * yy;
         }];
    
    
    // Start the physics timer
    timer = [NSTimer scheduledTimerWithTimeInterval: 0.03f target: self selector: @selector(tick) userInfo: nil repeats: YES];
}

- (void) initButterfly
{
    CGSize size;
    
    // Load the animation cells
    NSMutableArray *butterflies = [NSMutableArray array];
    for (int i = 1; i <= 17; i++)
    {
        NSString *fileName = [NSString stringWithFormat:@"bf_%d.png", i];
        UIImage *image = [UIImage imageNamed:fileName];
        size = image.size;
        [butterflies addObject:image];
    }
    
    // Begin the animation
    butterfly = [[UIImageView alloc] initWithFrame:(CGRect){.size=size}];
    [butterfly setAnimationImages:butterflies];
    butterfly.animationDuration = 0.75f;
    [butterfly startAnimating];
    
    // Set the butterfly's initial speed and acceleration
    xaccel = 2.0f;
    yaccel = 2.0f;
    xvelocity = 0.0f;
    yvelocity = 0.0f;
    
    // Add the butterfly
    butterfly.center = RECTCENTER(self.view.bounds);
    [self.view addSubview:butterfly];
}

- (void) loadView
{
    [super loadView];
    self.view.backgroundColor = [UIColor whiteColor];
    [self initButterfly];
}

免责声明:文章转载自《IOS 特定于设备的开发:Core Motion基础》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇using五大用法python_30期【函数里面不定长参数/动态参数 *arges**关键字参数**kwargs】下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Python urllib和urllib2模块学习(一)

(参考资料:现代魔法学院 http://www.nowamagic.net/academy/detail/1302803) Python标准库中有许多实用的工具类,但是在具体使用时,标准库文档上对使用细节描述的并不清楚,比如 urllib和urllib2 这个 HTTP 客户端库。这里总结了一些 urllib和urlib2 库的使用细节。 Python u...

Golang服务器热重启、热升级、热更新(safe and graceful hot-restart/reload http server)详解

服务端代码经常需要升级,对于线上系统的升级常用的做法是,通过前端的负载均衡(如nginx)来保证升级时至少有一个服务可用,依次(灰度)升级。 而另一种更方便的方法是在应用上做热重启,直接更新源码、配置或升级应用而不停服务。 这个功能在重要业务上尤为重要,会影响服务可用性、用户体验。 原理 热重启的原理比较简单,但是涉及到一些系统调用以及父子进程之间文件句...

Windows 的 80 端口被 System 进程占用解决方案

通过 Windows 的资源监视器(win+R:resmon)可以看到 80 端口已经被占用,下图是已经解决好了,没能截图被占用的情况,下面给出解决方案。 查看解决方案请直接跳转到第2节。 1、使用 windows 工具/命令查看端口占用情况 PS:贴出两个好用的 windows cmd 命令 查看占用端口程序的 PID(最后一列代表 PID): n...

.net core 学习小结之 配置介绍(config)以及热更新

命令行的配置 var settings = new Dictionary<string, string>{ { "name","cyao"}, {"age","18"} }; var builder = new Configura...

MongoDB文档操作(5)

添加文档 语法: db.集合名.insert({k1:"v1", k2:"v2"....}) 注意: (1)文档就是键值对,数据类型是BSON格式,支持的值更加丰富。 比如:db.集合名.insert({name:"bashlog", spc:{weight:100, address:"henan"}}) (2)在添加的文档里面,都有一个'_id'的键,...

IOS 特定于设备的开发:使用加速器启动屏幕上的对象

借助一点编程工作,iPhone的机载加速计就可以使对象在屏幕上四处“移动”,实时响应用户倾斜手机的方式。下面的代码就是创建一个动画式的蝴蝶,用户可以使之快速移过屏幕。 使之工作的秘密在于:向程序中添加一个所谓的"物理计时器“。他不是直接响应加速中的变化,而是加速计回调用于测量当前的力。它取决于计时器例程随着 时间的推移通过改变他的画面对蝴蝶应用那些力。下面是...