美文网首页
检测屏幕旋转-通过CoreMotion

检测屏幕旋转-通过CoreMotion

作者: frola_ | 来源:发表于2017-10-12 11:34 被阅读0次
    #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];
    }
    

    相关文章

      网友评论

          本文标题:检测屏幕旋转-通过CoreMotion

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