检测屏幕旋转-通过CoreMotion

#import <UIKit/UIKit.h>
#import <CoreMotion/CoreMotion.h>
//#import "NSString+Extension.h"

@interface ViewController : UIViewController
@property(nonatomic,strong)CMMotionManager *motionManager;
@end

判断是否旋转
给大家一个提示:commitAnimations记得在动画结束之后一定要commit,因为这个错误导致整个程序都错了,找了半天。

@implementation ViewController{
    CameraIconView *xiangCeIcon;
    CameraIconView *piaoJiaIcon;
    UILabel *directionPromptLabel1;
    UILabel *directionPromptLabel2;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self startMotionManager];
}

-(void)startMotionManager{
    if (_motionManager == nil) {
        _motionManager = [[CMMotionManager alloc]init];
        
    }
    _motionManager.accelerometerUpdateInterval = 0.3;
    
    if (_motionManager.deviceMotionAvailable) {
        NSLog(@"Device Motion Available");
        [_motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]
                                            withHandler: ^(CMDeviceMotion *motion, NSError *error){
                                                [self performSelectorOnMainThread:@selector(handleDeviceMotion:) withObject:motion waitUntilDone:YES];
                                                
                                            }];
    } else {
        NSLog(@"No device motion on device.");
        [self setMotionManager:nil];
    }
    
    
}

-(void)handleDeviceMotion:(CMDeviceMotion *)deviceMotion{
    double x = deviceMotion.gravity.x;
    double y = deviceMotion.gravity.y;
    if (fabs(y) >= fabs(x))
    {
        NSLog(@"竖屏");
        if (y >= 0){
            // UIDeviceOrientationPortraitUpsideDown;home键在上
            [self fanzhuan:M_PI];
            
            directionPromptLabel1.hidden = YES;
            directionPromptLabel2.hidden = NO;
        }
        else{
            // UIDeviceOrientationPortrait;home键在下
            [self fanzhuan:M_PI *2];
            
            directionPromptLabel1.hidden = NO;
            directionPromptLabel2.hidden = YES;
        }
    
    }
    else
    {
        NSLog(@"横屏");
        if (x >= 0){
            // UIDeviceOrientationLandscapeRight;//home键在右
            [self fanzhuan:-M_PI/2];
        }
        else{
            // UIDeviceOrientationLandscapeLeft;//home键在左
            [self fanzhuan:M_PI/2];
        }
        directionPromptLabel1.hidden = YES;
        directionPromptLabel2.hidden = NO;     
    }
    
}
-(void)fanzhuan:(CGFloat)angle{
    [CameraIconView beginAnimations:nil context:nil];
    [CameraIconView setAnimationDuration:0.5];
    
    CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
    [_PhotoButton setTransform:transform];
    [xiangCeIcon setTransform:transform ];
    [piaoJiaIcon setTransform:transform ];
    
    [CameraIconView commitAnimations];
}

退出的时候关闭调用

-(void)viewDidDisappear:(BOOL)animated{
      [_motionManager stopDeviceMotionUpdates];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,789评论 25 709
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 12,719评论 6 30
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 10,531评论 5 13
  • 前言 本文只要描述了iOS中的Core Animation(核心动画:隐式动画、显示动画)、贝塞尔曲线、UIVie...
    GitHubPorter阅读 9,004评论 7 11
  • 姓名:张丽娟 公司:个体 【日精进打卡第36天】 【知~学习】 《六项精进》2遍,共66遍 《大学》2遍,共54遍...
    兑了水的悲伤阅读 1,306评论 0 1