iOS development-CMMotionManager sensor gyroscope

iOS development-CMMotionManager sensor gyroscope

In the previous development, it was necessary to use the gyroscope to judge whether to take a picture or not to judge the level. If there is no level to take a picture, a prompt will be given. It is convenient for users to take pictures of suitable topics.

1. CM Motion Manager

What is CMMotionManager
CMMotionManager is the core class of the Core Motion library, which is responsible for obtaining and processing the motion information of the mobile phone. The data it can obtain are

  • Gyroscope, which identifies the instantaneous rotation of the device on the three main axes
  • Acceleration, which identifies the instantaneous acceleration of the device in three-dimensional space
  • Magnetic field information, identifying the orientation of the device relative to the Earth's magnetic field

Device motion data, identifying key motion-related attributes, including device user-induced acceleration, attitude, rotation rate, orientation relative to the calibration magnetic field, and orientation relative to gravity, etc. These data come from Core Motion's sensor fusion algorithm, from This data interface can obtain the above three kinds of data, so it is widely used

Two, CMMotionManager gyroscope code

The gyroscope judges whether to take a photo or not, and the level judgment
code is as follows

#import <Foundation/Foundation.h>
#import <CoreMotion/CoreMotion.h>

@interface SDSensorManager : NSObject

@property (nonatomic, copy) void (^updateDeviceMotionBlock)(CMDeviceMotion *data);

+ (instancetype)shareInstance;

- (void)startGyroscope;

- (void)stopGyroscope;

@end
#import "SDSensorManager.h"

static SDSensorManager *shareInstance = nil;

@interface SDSensorManager ()

@property (nonatomic, strong) CMMotionManager *motionManager;

@end

@implementation SDSensorManager

+ (instancetype)shareInstance {
    
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    
    
        shareInstance = [[SDSensorManager alloc] init];
        shareInstance.motionManager = [[CMMotionManager alloc]init];
    });
    return shareInstance;
}

- (void)startGyroscope {
    
    
    if (_motionManager.deviceMotionAvailable) {
    
    
        _motionManager.deviceMotionUpdateInterval = 1/30;
        __weak typeof(self)mySelf = self;
        [_motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
                                            withHandler:^(CMDeviceMotion *data, NSError *error) {
    
    
            if (mySelf.updateDeviceMotionBlock) {
    
    
                mySelf.updateDeviceMotionBlock(data);
            }
        }];
    }
}

- (void)stopGyroscope {
    
    
    [_motionManager stopDeviceMotionUpdates];
    self.updateDeviceMotionBlock = nil;
}

@end

Turn on the judging gyroscope, and judge whether to give corresponding prompts according to the horizontal rotation of the device.

/**
 启动陀螺仪
 */
- (void)startSenorManager {
    
    
    __weak typeof(self) weakSelf = self;
        
    [SDSensorManager shareInstance].updateDeviceMotionBlock = ^(CMDeviceMotion *data){
    
    
        
        DebugLog(@"gravity.x == %f", data.gravity.x);
        DebugLog(@"gravity.y == %f", data.gravity.y);
   
        CGFloat gravityX = data.gravity.x;
        CGFloat gravityY = data.gravity.y;
        
        BOOL hXShow = NO;
        BOOL hYShow = NO;

        if (gravityX > 0.20 || gravityX < -0.20) {
    
    
            // 水平倾斜了
            hXShow = YES;
        }
        
        if (gravityY > 0.20 || gravityY < -0.20) {
    
    
            // 竖直倾斜了
            hYShow = YES;
        }
        
        BOOL showCampass = NO;
        if (hXShow || hYShow) {
    
    
            // 水平倾斜了,可显示CampassImageView
            if (weakSelf.showExampleImage || weakSelf.showFrontImageView.image) {
    
    
                // 已经显示了example图片了,则不显示
                showCampass = NO;
            } else {
    
    
                showCampass = YES;
            }
        }
        
        weakSelf.compassImageView.hidden = !showCampass;
    };
    [[SDSensorManager shareInstance] startGyroscope];
}

Turn off the gyroscope, turn off the gyroscope when not in use.

/**
 关闭陀螺仪
 */
- (void)stopSenorManager {
    
    
    [[SDSensorManager shareInstance] stopGyroscope];
}

3. Summary

iOS development-CMMotionManager sensor gyroscope, use CMMotionManager to judge data such as gyroscope, acceleration, magnetic field information.

Learning records, keep improving every day.

Guess you like

Origin blog.csdn.net/gloryFlow/article/details/131968232