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

编程入门 行业动态 更新时间:2024-10-15 16:21:07

OC常用知识点之<a href=https://www.elefans.com/category/jswz/34/1745279.html style=陀螺仪测方向"/>

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

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

//
//  BHMotionOrientation.h
//  MotionOrientationDemo
//
//  Created by JasonHam on 2021/9/13.
//#import <Foundation/Foundation.h>NS_ASSUME_NONNULL_BEGINtypedef 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;@endNS_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常用知识点之陀螺仪测方向

本文发布于:2024-03-07 20:27:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1718838.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:陀螺仪   知识点   方向   常用   OC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!