OC常用知识点之陀螺仪测方向

Posted hbblzjy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OC常用知识点之陀螺仪测方向相关的知识,希望对你有一定的参考价值。

简单记录一下,封装了一个BHMotionOrientation类,以供大家参考,当然demo也添加了一view的基础旋转。

//
//  BHMotionOrientation.h
//  MotionOrientationDemo
//
//  Created by JasonHam on 2021/9/13.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSInteger, BHDirectionType) {
    ///未知
    BHDirectionType_Unknow = 0,
    ///竖直
    BHDirectionType_Portrait = 1,
    ///倒转
    BHDirectionType_Down = 2,
    ///左
    BHDirectionType_Left = 3,
    ///右
    BHDirectionType_Right = 4,
};

//自定义delegate
@class BHMotionOrientation;
@protocol BHMotionOrientationDelegate <NSObject>

///方向改变
- (void)motionOrientationDidChange:(BHMotionOrientation * _Nullable)motionOrientation direction:(BHDirectionType)direction;

@end

@interface BHMotionOrientation : NSObject

///代理
@property (nonatomic, weak)id <BHMotionOrientationDelegate>delegate;

///开启陀螺仪
-(void)startMotion;

///停止陀螺仪
-(void)stopMotion;

@end

NS_ASSUME_NONNULL_END
//
//  BHMotionOrientation.m
//  MotionOrientationDemo
//
//  Created by JasonHam on 2021/9/13.
//

#import "BHMotionOrientation.h"

#import <CoreMotion/CoreMotion.h>

///灵敏度
static const float  sensitive = 0.80;

@interface BHMotionOrientation ()

///陀螺仪管理者
@property (nonatomic, strong) CMMotionManager *motionManager;
///方向
@property (nonatomic, assign) BHDirectionType direction;

@end

@implementation BHMotionOrientation

#pragma mark - getter
-(CMMotionManager *)motionManager{
    
    if (!_motionManager) {
        _motionManager = [[CMMotionManager alloc] init];
        //更新间隔时间
        _motionManager.deviceMotionUpdateInterval = 0.025f;
    }
    return _motionManager;
}

#pragma mark - 开启陀螺仪
-(void)startMotion{
    
    if (self.motionManager.deviceMotionAvailable) {
        
        __weak typeof(self) wself = self;
        [_motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) {
            
            [wself changeDirectionWithMotion:motion];
            
        }];
        
    }
}

#pragma mark - 更改方向
-(void)changeDirectionWithMotion:(CMDeviceMotion *)motion{
    
    double x = motion.gravity.x;
    double y = motion.gravity.y;
    
    if (y < 0) {
        if (fabs(y) > sensitive) {
            if (_direction != BHDirectionType_Portrait) {
                
                _direction = BHDirectionType_Portrait;
                
                if (_delegate && [_delegate respondsToSelector:@selector(motionOrientationDidChange:direction:)]) {
                    [_delegate motionOrientationDidChange:self direction:_direction];
                }
            }
        }
    } else {
        if (y > sensitive) {
            if (_direction != BHDirectionType_Down) {
                
                _direction = BHDirectionType_Down;
                
                if (_delegate && [_delegate respondsToSelector:@selector(motionOrientationDidChange:direction:)]) {
                    [_delegate motionOrientationDidChange:self direction:_direction];
                }
            }
        }
    }
    
    if (x < 0) {
        if (fabs(x) > sensitive) {
            if (_direction != BHDirectionType_Left) {
                
                _direction = BHDirectionType_Left;
                
                if (_delegate && [_delegate respondsToSelector:@selector(motionOrientationDidChange:direction:)]) {
                    [_delegate motionOrientationDidChange:self direction:_direction];
                }
            }
        }
    } else {
        if (x > sensitive) {
            if (_direction != BHDirectionType_Right) {
                
                _direction = BHDirectionType_Right;
                
                if (_delegate && [_delegate respondsToSelector:@selector(motionOrientationDidChange:direction:)]) {
                    [_delegate motionOrientationDidChange:self direction:_direction];
                }
            }
        }
    }
}

#pragma mark - 停止陀螺仪
-(void)stopMotion{
    
    [_motionManager stopDeviceMotionUpdates];
    
}


@end

 Demo地址:GitHub - hbblzjy/MotionOrientationDemoContribute to hbblzjy/MotionOrientationDemo development by creating an account on GitHub.https://github.com/hbblzjy/MotionOrientationDemo

以上是关于OC常用知识点之陀螺仪测方向的主要内容,如果未能解决你的问题,请参考以下文章

IOS开发-OC学习-常用功能代码片段整理

Human Motion Analysis with Wearable Inertial Sensors——阅读3

ios开发之OC基础-类和对象

vue-router 2.0 常用基础知识点之router-link

OC基础--常用类的初步介绍与简单实用之NSString

三轴速率陀螺装在俯仰轴上,怎么计算方位速率