美文网首页
iOS旋转处理的几种方式

iOS旋转处理的几种方式

作者: 里克尔梅西 | 来源:发表于2020-08-17 13:34 被阅读0次

iOS的屏幕旋转处理有很多的方式,慢慢的在后续中一一记录一下

一、采用陀螺仪监听的方式

在项目中用到模仿小猿搜题拍照识别页面,页面中会有根据相机方向的旋转,来对图标进行动画旋转,采用了陀螺仪监听的方式
具体代码如下:
.h文件

#import <Foundation/Foundation.h>
typedef NS_ENUM(NSInteger,TgDirection) {
    TgDirectionUnkown,
    TgDirectionPortrait,
    TgDirectionDown,
    TgDirectionRight,
    TgDirectionLeft,
};

NS_ASSUME_NONNULL_BEGIN

@protocol DeviceOrientationDelegate <NSObject>

- (void)directionChange:(TgDirection)direction;

@end
@interface DeviceOrientationManager : NSObject

@property(nonatomic,strong)id<DeviceOrientationDelegate>delegate;
@property (nonatomic, assign) TgDirection currentDirection;

- (instancetype)initWithDelegate:(id<DeviceOrientationDelegate>)delegate;
/**
 开启监听
 */
- (void)startMonitor;
/**
 结束监听,请stop
 */
- (void)stop;

@end

NS_ASSUME_NONNULL_END

.m文件

#import "DeviceOrientationManager.h"
#import <CoreMotion/CoreMotion.h>

@interface DeviceOrientationManager () {
    
    CMMotionManager *_motionManager;
}
@end
//sensitive 灵敏度
static const float sensitive = 0.77;

@implementation DeviceOrientationManager

- (instancetype)initWithDelegate:(id<DeviceOrientationDelegate>)delegate {
    self = [super init];
    if (self) {
        
        _delegate = delegate;
    }
    return self;
}
- (void)startMonitor {
    
    [self start];
}

- (void)stop {
    [_motionManager stopDeviceMotionUpdates];
}


//陀螺仪 每隔一个间隔做轮询
- (void)start{
    
    if (_motionManager == nil) {
        
        _motionManager = [[CMMotionManager alloc] init];
    }
    _motionManager.deviceMotionUpdateInterval = 1/40.f;
    if (_motionManager.deviceMotionAvailable) {
        
        [_motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]
                                            withHandler: ^(CMDeviceMotion *motion, NSError *error){
                                                [self performSelectorOnMainThread:@selector(deviceMotion:) withObject:motion waitUntilDone:YES];
                                            }];
    }
}
- (void)deviceMotion:(CMDeviceMotion *)motion{
    
    double x = motion.gravity.x;
    double y = motion.gravity.y;

    if (y < 0 ) {
        if (fabs(y) > sensitive) {
            if (self.currentDirection != TgDirectionPortrait) {
                self.currentDirection = TgDirectionPortrait;
                if ([self.delegate respondsToSelector:@selector(directionChange:)]) {
                    [self.delegate directionChange:self.currentDirection];
                }
            }
        }
    }else {
        if (y > sensitive) {
            if (self.currentDirection != TgDirectionDown) {
                self.currentDirection = TgDirectionDown;
                if ([self.delegate respondsToSelector:@selector(directionChange:)]) {
                    [self.delegate directionChange:self.currentDirection];
                }
            }
        }
    }
    if (x < 0 ) {
        if (fabs(x) > sensitive) {
            if (self.currentDirection != TgDirectionLeft) {
                self.currentDirection = TgDirectionLeft;
                if ([self.delegate respondsToSelector:@selector(directionChange:)]) {
                    [self.delegate directionChange:self.currentDirection];
                }
            }
        }
    }else {
        if (x > sensitive) {
            if (self.currentDirection != TgDirectionRight) {
                self.currentDirection = TgDirectionRight;
                if ([self.delegate respondsToSelector:@selector(directionChange:)]) {
                    [self.delegate directionChange:self.currentDirection];
                }
            }
        }
    }
}
@end

使用方法

 self.orientationManager = [[DeviceOrientationManager alloc] initWithDelegate:self];
 [self.orientationManager startMonitor];

#pragma mark - DeviceOrigatrion Delegate
- (void)directionChange:(TgDirection)direction {
    
    switch (direction) {
        case TgDirectionPortrait:
            
            NSLog(@"protrait");
            self.currentDirection = TgDirectionPortrait;
            self.maskView.hidden = NO;
//            [self rotationAnimation];

            break;
        case TgDirectionDown:
            self.maskView.hidden = NO;

            NSLog(@"down");

            break;
        case TgDirectionRight:
            self.maskView.hidden = NO;

            NSLog(@"right");
            self.currentDirection = TgDirectionRight;
//            [self rotationAnimation];
            
            break;
        case TgDirectionLeft:
            self.maskView.hidden = YES;

            NSLog(@"left");
            self.currentDirection = TgDirectionLeft;
//            [self rotationAnimation];
            
            break;
    
        default:
            break;
    }
}

相关文章

  • iOS旋转处理的几种方式

    iOS的屏幕旋转处理有很多的方式,慢慢的在后续中一一记录一下 一、采用陀螺仪监听的方式 在项目中用到模仿小猿搜题拍...

  • iOS-屏幕旋转截屏相关

    本篇收录各种屏幕旋转知识点等. 1.详解iOS开发中处理屏幕旋转的几种方法2.iOS 个别页面强制横屏,其他页面竖...

  • iOS音频播放的几种方式

    title : iOS音频播放的几种方式category : IOS iOS音频播放的几种方式 标签(空格分隔)...

  • iOS监听屏幕旋转的几种方式

    1. 使用viewWillTransitionToSize:withTransitionCoordinator: ...

  • masonry 源码解读

    ios 手写布局的几种方式 Frame AutoLayout VFL Masonry ios 布局的几种方式 1....

  • iOS 知识点归纳-一些点

    1 崩溃处理 iOS开发中碰到的几种常见的崩溃方式 - 简书[https://www.jianshu.com/p/...

  • HOOK原理

    hook(钩子)处理特殊的消息机制 iOS中HOOK技术的几种方式 1、Method Swizzle利用OC的Ru...

  • iOS 几种圆角处理方式

    1.通过设置layer的属性 -最简单的一种,但是很影响性能 Core Graphics方式 -用贝塞尔曲线UIB...

  • 物体的几种旋转方式

    一种错误的写法 可以看到物体只有一开始在 X 轴上是有旋转的,后面就 “卡住了”,具体的原因可以在学习了四元数与欧...

  • iOS开发之UITableView中计时器的几种实现方式

    iOS开发之UITableView中计时器的几种实现方式 iOS开发之UITableView中计时器的几种实现方式

网友评论

      本文标题:iOS旋转处理的几种方式

      本文链接:https://www.haomeiwen.com/subject/hpoodktx.html