iOS旋转处理的几种方式
2020-08-17 本文已影响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;
}
}