天天看點

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.

OC常用知識點之陀螺儀測方向

https://github.com/hbblzjy/MotionOrientationDemo

繼續閱讀