ios long转float_iOS旋转处理的几种方式

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

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

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

具体代码如下:

.h文件

#import

typedef NS_ENUM(NSInteger,TgDirection) {

TgDirectionUnkown,

TgDirectionPortrait,

TgDirectionDown,

TgDirectionRight,

TgDirectionLeft,

};

NS_ASSUME_NONNULL_BEGIN

@protocol DeviceOrientationDelegate

- (void)directionChange:(TgDirection)direction;

@end

@interface DeviceOrientationManager : NSObject

@property(nonatomic,strong)iddelegate;

@property (nonatomic, assign) TgDirection currentDirection;

- (instancetype)initWithDelegate:(id)delegate;

/**

开启监听

*/

- (void)startMonitor;

/**

结束监听,请stop

*/

- (void)stop;

@end

NS_ASSUME_NONNULL_END

.m文件

#import "DeviceOrientationManager.h"

#import

@interface DeviceOrientationManager () {

CMMotionManager *_motionManager;

}

@end

//sensitive 灵敏度

static const float sensitive = 0.77;

@implementation DeviceOrientationManager

- (instancetype)initWithDelegate:(id)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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值