檢測屏幕旋轉-通過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];
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,781評論 25 709
  • 在iOS中隨處都可以看到絢麗的動畫效果,實現這些動畫的過程并不復雜,今天將帶大家一窺ios動畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,688評論 6 30
  • 在iOS中隨處都可以看到絢麗的動畫效果,實現這些動畫的過程并不復雜,今天將帶大家一窺iOS動畫全貌。在這里你可以看...
    F麥子閱讀 5,261評論 5 13
  • 前言 本文只要描述了iOS中的Core Animation(核心動畫:隱式動畫、顯示動畫)、貝塞爾曲線、UIVie...
    GitHubPorter閱讀 3,736評論 7 11
  • 姓名:張麗娟 公司:個體 【日精進打卡第36天】 【知~學習】 《六項精進》2遍,共66遍 《大學》2遍,共54遍...
    兌了水的悲傷閱讀 230評論 0 1

友情鏈接更多精彩內容