<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont

简介: 引进框架: #import 定义属性初始化相关: #import "ViewController.h"#import @interface ViewController ()@property (...
引进框架:

#import <CoreMotion/CoreMotion.h>

定义属性初始化相关:

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

@interface ViewController ()

@property (nonatomic, strong) CMMotionManager *motionManager;

@property (nonatomic, strong) NSOperationQueue *quene;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   // 初始化 CMMotionManager
    self.motionManager = [[CMMotionManager alloc]init];
    
    // 初始化 NSOperationQueue
    self.quene = [[NSOperationQueue alloc]init];
    
    // 调用加速器
    [self configureAccelerometer];
    
    // 调用陀螺仪
    [self configureGrro];
 
}
每日更新关注 : http://weibo.com/hanjunqiang   新浪微博
加速器的使用:

/*
// 每一个设备晃动的时候, 系统通知每一个在用的设备, 可以使本身成为第一响应者
- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (void)viewDidAppear:(BOOL)animated
{
    [self becomeFirstResponder];
}
 */


// 加速器的方法
- (void)configureAccelerometer
{
    /**
     * 5.0之前使用的是pull方式,之后使用push方式
     *
    // pull 方式
    // 判断加速器是否可以使用
    if ([_motionManager isAccelerometerAvailable]) {
        [_motionManager setAccelerometerUpdateInterval:1 / 40.0];
        [_motionManager startAccelerometerUpdates];
    }else{
        NSLog(@"加速器不能使用");
    }
    */
     
    // push 方式
    if ([_motionManager isAccelerometerAvailable]) {
        // 设置加速器的频率
        [_motionManager setAccelerometerUpdateInterval:1 / 40.0];
        // 开始采集数据
        [_motionManager startAccelerometerUpdatesToQueue:_quene withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
            
            if (fabs(accelerometerData.acceleration.x) > 2.0 || fabs(accelerometerData.acceleration.y) > 2.0 || fabs(accelerometerData.acceleration.z) > 2.0) {
                NSLog(@"检测到震动");
            }
            NSLog(@"%.2f__%.2f__%.2f",accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z);
         
        }];
    }else{
        NSLog(@"加速器不能使用");
    }
    
    
}

// 触摸结束的时候
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CMAcceleration acceleration = _motionManager.accelerometerData.acceleration;
    NSLog(@"%.2f__%.2f__%.2f",acceleration.x,acceleration.y,acceleration.z);
}

陀螺仪的使用:

// 陀螺仪的使用
- (void)configureGrro
{
    if ([_motionManager isGyroAvailable]) {
        [self.motionManager startGyroUpdatesToQueue:_quene withHandler:^(CMGyroData *gyroData, NSError *error) {
            
            NSLog(@"%.2f__%.2f__%.2f",gyroData.rotationRate.x,gyroData.rotationRate.y,gyroData.rotationRate.z);
            
        }];
    }else{
        NSLog(@"陀螺仪不能使用");
    }
}

晃动触发的一些方法:

- (void)viewDidDisappear:(BOOL)animated
{
    [self.motionManager stopAccelerometerUpdates];
    [self.motionManager stopGyroUpdates];
}

// 开始晃动的时候触发
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"开始晃动");
}

// 结束晃动的时候触发
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"晃动结束");
}

// 中断晃动的时候触发
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"取消晃动,晃动终止");
}

每日更新关注 : http://weibo.com/hanjunqiang   新浪微博

目录
相关文章
|
Web App开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
TCP洪水攻击(SYN Flood)的诊断和处理 Posted by  海涛  on 2013 年 7 月 11 日 Tweet1 ​1. SYN Flood介绍 前段时间网站被攻击多次,其中最猛烈的就是TCP洪水攻击,即SYN Flood。
1172 0
|
Web App开发 存储 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
      前段时间公司hadoop集群宕机,发现是namenode磁盘满了, 清理出部分空间后,重启集群时,重启失败。 又发现集群Secondary namenode 服务也恰恰坏掉,导致所有的操作log持续写入edits.new 文件,等集群宕机的时候文件大小已经达到了丧心病狂的70G+..重启集群报错 加载edits文件失败。
1063 0
|
Web App开发 监控 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
Datanode的日志中看到: 10/12/14 20:10:31 INFO hdfs.DFSClient: Could not obtain block blk_XXXXXXXXXXXXXXXXXXXXXX_YYYYYYYY from any node: java.
790 0
|
Web App开发 数据库
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
可伸缩系统的架构经验 Feb 27th, 2013 | Comments 最近,阅读了Will Larson的文章Introduction to Architecting System for Scale,感觉很有价值。
2458 0
|
Web App开发 前端开发 程序员
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
Facebook 内部分享:不论你如何富有,你都赚不到更多的时间,你也回不到过去。没有那么多的假如,只有指针滴答的时光飞逝和你应该好好把握的现在,以下25张PPT的分享将为您带来时间价值管理的技巧。
696 0
|
Web App开发 前端开发 测试技术
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
ClusterId read in ZooKeeper is null. Re-running the program after fixing issue 1 will result in the following e...
906 0
|
Web App开发 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
一个典型的星型模式包括一个大型的事实表和一组逻辑上围绕这个事实表的维度表。  事实表是星型模型的核心,事实表由主键和度量数据两部分组成。
618 0
|
Web App开发 前端开发 大数据
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
提  纲     1、移动DSP与传统营销有什么不同?     2、为什么移动DSP是大势所趋?     3、哪些因素决定移动DSP的精准与否?     4、如何辨别移动DSP的真伪优劣?     ...
1076 0

热门文章

最新文章