日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2025/3/17 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS中 陀螺仪/加速器 韩俊强的博客 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
引進(jìn)框架:

#import <CoreMotion/CoreMotion.h>

定義屬性初始化相關(guān):

#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];// 初始化 CMMotionManagerself.motionManager = [[CMMotionManager alloc]init];// 初始化 NSOperationQueueself.quene = [[NSOperationQueue alloc]init];// 調(diào)用加速器[self configureAccelerometer];// 調(diào)用陀螺儀[self configureGrro];} 每日更新關(guān)注:http://weibo.com/hanjunqiang? 新浪微博
加速器的使用:

/* // 每一個設(shè)備晃動的時候, 系統(tǒng)通知每一個在用的設(shè)備, 可以使本身成為第一響應(yīng)者 - (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]) {// 設(shè)置加速器的頻率[_motionManager setAccelerometerUpdateInterval:1 / 40.0];// 開始采集數(shù)據(jù)[_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(@"加速器不能使用");}}// 觸摸結(jié)束的時候 - (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(@"陀螺儀不能使用");} }
晃動觸發(fā)的一些方法:

- (void)viewDidDisappear:(BOOL)animated {[self.motionManager stopAccelerometerUpdates];[self.motionManager stopGyroUpdates]; }// 開始晃動的時候觸發(fā) - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {NSLog(@"開始晃動"); }// 結(jié)束晃動的時候觸發(fā) - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {NSLog(@"晃動結(jié)束"); }// 中斷晃動的時候觸發(fā) - (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {NSLog(@"取消晃動,晃動終止"); }
每日更新關(guān)注:http://weibo.com/hanjunqiang? 新浪微博

總結(jié)

以上是生活随笔為你收集整理的iOS中 陀螺仪/加速器 韩俊强的博客的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。