1. 程式人生 > >iOS 螢幕旋轉監聽

iOS 螢幕旋轉監聽

方法一:通知監聽

//UIApplicationWillChangeStatusBarFrameNotification 將要轉屏
//UIApplicationDidChangeStatusBarFrameNotification 已經轉屏

// 轉屏通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didChangeRotate:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];

- (void)didChangeRotate:(NSNotification*)notice {
    if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait
        || [[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortraitUpsideDown) {
        //豎屏
    } else {
        //橫屏
        }
   }

2、重寫方法

// 橫豎屏將要切換會呼叫
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    NSLog(@"橫豎屏進行了切換size:%@",NSStringFromCGSize(size)); 
    // 延時一下 獲得的高度才正確,要不然是轉屏前的寬高
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           
    });
}