第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定

iOS/WatchOS 加速度數(shù)據(jù)獲取

標(biāo)簽:
iOS

最近在做一个有意思的App,需要用到iPhone或者手表的加速度数据
所以总结一下分享给大家

使用加速度数据非常简单,首先引用头文件:

#import <CoreMotion/CoreMotion.h>

在当前的ViewController中添加一个strong属性的CMMotionManager实例

@property (nonatomic, strong) CMMotionManager *manager;

之所以强调使用强引用,是因为我一开始就入坑了,如果是局部变量,这个实例就会在数据反馈前被销毁,导致一直没有反馈数据。

然后初始化manager对象:

_manager = [[CMMotionManager alloc] init];

之后判断能不能用加速度传感器(陀螺仪传感器同理)

if (_manager.accelerometerAvailable) {
    //可以使用加速度传感器
}

然后,设置加速度数据获取的时间间隔:

_manager.accelerometerUpdateInterval = 0.1;

我们这里取100ms

最后,就是更新数据了:

[_manager startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc]init] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
            CMAcceleration acc = accelerometerData.acceleration;
            //NSLog(@"%f,%f,%f",acc.x,acc.y,acc.x);
            NSString* x = [NSString stringWithFormat:@"x:%lf",acc.x];
            NSString* y = [NSString stringWithFormat:@"y:%lf",acc.y];
            NSString* z = [NSString stringWithFormat:@"z:%lf",acc.z];
 }];

这就是整个获取的过程,非常简单

附上全部代码:

//
//  InterfaceController.m
//  WatchMeature WatchKit Extension
//
//  Created by Realank on 16/1/25.
//  Copyright © 2016年 realank. All rights reserved.
//

#import "InterfaceController.h"
#import <CoreMotion/CoreMotion.h>
#import <WatchConnectivity/WatchConnectivity.h>
#import <HealthKit/HealthKit.h>

@interface InterfaceController()<WCSessionDelegate,HKWorkoutSessionDelegate>

@property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceLabel *xLabel;
@property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceLabel *yLabel;
@property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceLabel *zLabel;

@property (nonatomic, strong) CMMotionManager *manager;
@property (nonatomic, strong) HKHealthStore *healthStore;
@end

@implementation InterfaceController

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];

    HKWorkoutSession *session = [[HKWorkoutSession alloc]initWithActivityType:HKWorkoutActivityTypeRunning  locationType:HKWorkoutSessionLocationTypeOutdoor];
    session.delegate = self;
    [self.healthStore startWorkoutSession:session];

}

- (void)willActivate {
    // This method is called when watch view controller is about to be visible to user
    [super willActivate];

    WCSession* session = [WCSession defaultSession];
    session.delegate = self;
    [session activateSession];

    _manager = [[CMMotionManager alloc] init];
    if (_manager.accelerometerAvailable) {
        NSLog(@"accelerometerAvailable");
        _manager.accelerometerUpdateInterval = 0.1;
        __block BOOL sendSuccess = YES;
        [_manager startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc]init] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
            CMAcceleration acc = accelerometerData.acceleration;
            //NSLog(@"%f,%f,%f",acc.x,acc.y,acc.x);
            NSString* x = [NSString stringWithFormat:@"x:%lf",acc.x];
            NSString* y = [NSString stringWithFormat:@"y:%lf",acc.y];
            NSString* z = [NSString stringWithFormat:@"z:%lf",acc.z];

            dispatch_async(dispatch_get_main_queue(), ^{

                [self.xLabel setText:x];
                [self.yLabel setText:y];
                [self.zLabel setText:z];
                if (sendSuccess) {
                    sendSuccess = NO;
                    NSDictionary *dict = [NSDictionary dictionaryWithObjects:@[x,y,z] forKeys:@[@"x",@"y",@"z"]];
                    [session sendMessage:dict replyHandler:^(NSDictionary<NSString *,id> * _Nonnull replyMessage) {
                        sendSuccess = YES;
                    } errorHandler:^(NSError * _Nonnull error) {

                    }];
                }

            });

        }];
    }

}

- (void)didDeactivate {
    // This method is called when watch view controller is no longer visible
    [super didDeactivate];
}

- (void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *,id> *)message {

}

- (void)workoutSession:(HKWorkoutSession *)workoutSession didFailWithError:(NSError *)error {

}

@end

iOS和WatchOS关于加速度传感器的数据获取,代码是一样的,
但是WatchOS好像不能获取陀螺仪数据,具体原因暂时还没有研究

點(diǎn)擊查看更多內(nèi)容
5人點(diǎn)贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
移動開發(fā)工程師
手記
粉絲
11
獲贊與收藏
446

關(guān)注作者,訂閱最新文章

閱讀免費(fèi)教程

感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊有機(jī)會得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報

0/150
提交
取消