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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hbblzjy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值