iOS中 陀螺仪/加速器 韩俊强的博客

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

#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   新浪微博

目录
相关文章
|
30天前
|
数据采集 网络协议 开发工具
如何进行iOS技术博客的备案?
如何进行iOS技术博客的备案?
24 2
|
4月前
|
网络安全 开发者 iOS开发
iOS技术博客:App备案指南
本文介绍了移动应用程序(App)备案的重要性和流程。备案是规范App开发和运营的必要手段,有助于保护用户权益、维护网络安全和社会秩序。为了帮助开发者更好地了解备案流程,本文提供了一份最新、最全、最详的备案指南,包括备案目的、好处、对象、时间、流程、条件和注意事项等内容。
iOS技术博客:App备案指南
|
5月前
|
数据采集 网络协议 开发工具
 如何进行iOS技术博客的备案?
在本篇问答中,我们将为iOS技术博主介绍如何进行备案。如果你的iOS应用只包含简单的页面,并通过蓝牙进行数据采集和传输,那么你可能不需要备案。然而,如果你希望通过域名调用后端服务,建议进行备案以满足国内服务器访问的要求。我们将详细解释备案的三要素以及备案流程,并提供参考资料供你查阅。
|
Web App开发 Dart 安全
flutter制作博客展示平台,现已支持 Web、macOS 应用、Android 和 iOS
Flutter Blog Theme using Flutter | Web, macOS, Android, iOS Flutter 最近发布了 Flutter V2.5.1,其性能得到了很大提升,支持 Web、macOS、Android 和 iOS。 这就是为什么今天我们使用在 Web、macOS 应用、Android 和 iOS 应用上运行的 flutter 创建响应式博客主题。 此外,我们创建了一个具有自定义悬停动画的动画网络菜单。 最后,您将学习如何使用 Flutter 制作响应式应用程序。
325 0
flutter制作博客展示平台,现已支持 Web、macOS 应用、Android 和 iOS
|
iOS开发
iOS中 断点下载详解 韩俊强的博客
布局如下: 基本拖拉属性: #import "ViewController.h" #import "AFNetworking.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UILab...
775 0
|
定位技术 iOS开发
iOS中 百度地图详解 韩俊强的博文
需要准备工作按照下图引进类库 需要添加 添加的两个字符串为:NSLocationWhenInUseUsageDescription  /  NSLocationAlwaysUsageDescription 默认定位设置: 设置工作准备完毕上代码: 指示根视图: ...
670 0
|
iOS开发 索引
iOS中 NSString的几种常用方法 韩俊强的博客
要把 “2011-11-29” 改写成 “2011/11/29”一开始想用ios的时间格式,后来用NSString的方法搞定。 1.创建NSString字符串 NSString 与 char* 最大的区别就是 NSString是一个objective对象,而char* 是一个字节数组。
774 0
|
iOS开发
IOS小工具以及精彩的博客
IOS小工具以及精彩的博客 工具Log Guru是一个收集Log的小工具, 可以在 Mac 上查看 iOS 设备的实时系统日志. 现在可以直接高亮显示在 FIR.im 上安装 app 失败的原因。后续会增加各种进程过滤、高亮某进程、只看 Error level 等功能。
946 0
|
Web App开发 iOS开发
IOS开发博客学习
M了个J :http://www.cnblogs.com/mjios/tag/objective-c/ http://www.cnblogs.com/tianjian/p/3358602.html  Cocos2d-x  VS环境配置  泰然论坛:http://www.
728 0
|
iOS开发
IOS学习博客不错的大部分是原创
http://blog.csdn.net/iukey/article/category/955062
578 0