iOS之屏幕旋轉(zhuǎn)(橫屏),看我就夠了

前言:

1.其實大多數(shù)app并不需要手動控制屏幕的旋轉(zhuǎn),甚至可能都是不允許旋轉(zhuǎn)的,但是如果涉及到視頻播放界面,那么想必一定會用到手動控制屏幕旋轉(zhuǎn),或者指定某個控制器能夠旋轉(zhuǎn).

2.這里我就把對于屏幕旋轉(zhuǎn)的所有情況的處理方法都列出出來,當然,重點是指定控制器旋轉(zhuǎn),并可手動控制

1.app不需要屏幕旋轉(zhuǎn)

如圖所示,只勾選Portrait即可.

Paste_Image.png

或者在APPdelegate.m中實現(xiàn)如下方法

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return UIInterfaceOrientationMaskPortrait;
}
2.app需要旋轉(zhuǎn)
(1)跟隨系統(tǒng)型,當手機解鎖屏幕旋轉(zhuǎn)時,app能夠旋轉(zhuǎn),關閉屏幕旋轉(zhuǎn)時app不能旋轉(zhuǎn)

如下圖所示即可


Paste_Image.png

或者在APPdelegate.m中實現(xiàn)如下方法

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return UIInterfaceOrientationMaskAll;
}

重點來了:

(2)隨心所欲型,可指定某個控制器能夠屏幕旋轉(zhuǎn),并可手動控制.

1.打開General,,設置如下


Paste_Image.png

2.給APPdelegate添加BOOL類型屬性

// APPdelegate.h 添加該屬性
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

// 判斷是否允許屏幕旋轉(zhuǎn)
@property (nonatomic,assign)BOOL allowRotation;

@end
// APPdelegate.m 實現(xiàn)如下方法
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    
    if (_allowRotation) {
        return UIInterfaceOrientationMaskAll;
    }else{
        return UIInterfaceOrientationMaskPortrait;
    }
}

3.在你需要旋轉(zhuǎn)的控制器內(nèi)導入AppDelegate.h文件

#import "AppDelegate.h"

- (void)viewDidDisappear:(BOOL)animated {
    
    [super viewDidDisappear:animated];
 
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.allowRotation = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    
    [super viewDidAppear:animated];

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.allowRotation = YES;
}
  • 此時已經(jīng)實現(xiàn)指定控制器開啟屏幕旋轉(zhuǎn),但此時仍是"跟隨系統(tǒng)型",也就是說還不能手動控制屏幕的旋轉(zhuǎn),當手機鎖定屏幕旋轉(zhuǎn)時,無法進行屏幕旋轉(zhuǎn).
  • 接下來便是實現(xiàn)手動控制,即使手機鎖定屏幕旋轉(zhuǎn),仍能控制屏幕的旋轉(zhuǎn)

4.手動控制橫屏.創(chuàng)建一個按鈕,點擊該按鈕實現(xiàn)屏幕的橫屏與豎屏,暫且稱該按鈕為btn

// MARK: 點擊btn按鈕
- (void)fullButtonClick:(UIButton *)sender {
    // 這里我是通過按鈕的selected狀態(tài)來判定橫屏豎屏的,并不是唯一的判斷標準
    if (sender.selected) {
        [self changeOrientation:UIInterfaceOrientationPortrait];
    }else{
        [self changeOrientation:UIInterfaceOrientationLandscapeRight];
    }
}

/**
 *    強制橫屏
 *
 *    @param orientation 橫屏方向
 */
- (void)changeOrientation:(UIInterfaceOrientation)orientation
{
    int val = orientation;
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}
  • 至此,我們便實現(xiàn)了指定控制器旋轉(zhuǎn)(橫屏),并可手動控制.此時可能有同學會問,我需要屏幕旋轉(zhuǎn)后進行其他處理,那么我應該在哪里處理,怎么處理呢?接下來,便是監(jiān)聽手機設備的屏幕旋轉(zhuǎn).

5.監(jiān)聽手機設備屏幕方向變化.

// 控制器的viewDidLoad方法
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 接收屏幕方向改變通知,監(jiān)聽屏幕方向
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationHandler) name:UIDeviceOrientationDidChangeNotification object:nil];
    
    [self setupUI];
}

// 通知方法
- (void)orientationHandler {
    
// 在這里可進行屏幕旋轉(zhuǎn)時的處理

    // 獲取當前設備方向
    UIDeviceOrientation orient = [UIDevice currentDevice].orientation;
    // 動畫時長
    NSTimeInterval duration = 0.3;
    // 獲取寬高
    CGFloat w = CGRectGetWidth(self.view.bounds);
    CGFloat h = CGRectGetHeight(self.view.bounds);
    
    // 處理方法,該方法參數(shù)根據(jù)跟人情況而定
    [self fullScreenWithUIDeviceOrientation:orient duration:duration width:w height:h];
    
}

// 處理橫屏豎屏
- (void)fullScreenWithUIDeviceOrientation:(UIDeviceOrientation)orientation duration:(NSTimeInterval)duration width:(CGFloat)width height:(CGFloat)height {
    
    if (orientation == UIDeviceOrientationFaceUp || orientation == UIDeviceOrientationFaceDown || orientation == UIDeviceOrientationPortraitUpsideDown) return;
    
    if (orientation == UIDeviceOrientationPortrait) {
        // 豎屏
        // 處理方法
    }else{
        // 向左旋轉(zhuǎn)或向右旋轉(zhuǎn)
        // 處理方法
    }
}

補充:UIDeviceOrientationFaceUp = 手機正面向上.不管你手機橫屏,還是豎屏,當手機屏幕向上時,獲取的設備方向都是FaceUp
    UIDeviceOrientationFaceDown = 手機正面向下.也就是不管你手機橫屏還是豎屏,當手機屏幕向下時,獲取的設備方向都是FaceDown.
    UIDeviceOrientationPortraitUpsideDown = 豎屏且頂部旋轉(zhuǎn)至home鍵方向,iPhone設備是不允許的,所以iPhone上不會出現(xiàn)這個參數(shù).但是iPad上可以.

當屏幕與水平線垂直時,旋轉(zhuǎn)時才會獲取到UIDeviceOrientationPortrait,UIDeviceOrientationLandscapeLeft,UIDeviceOrientationLandscapeRight三個參數(shù).
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容