iOS開(kāi)發(fā) 詳解強(qiáng)制屏幕旋轉(zhuǎn)的方法

第一步:

首先,我定義了一個(gè)變量isFullScreen,用于判斷當(dāng)前視圖是處于橫屏狀態(tài)還是豎屏狀態(tài)。YES為橫屏,NO為豎屏。

BOOL _isFullScreen

第二步:

我寫(xiě)了一個(gè)方法用于執(zhí)行轉(zhuǎn)屏的操作,不論是橫屏,還是豎屏操作都可以調(diào)用這個(gè)方法,里面會(huì)根據(jù)當(dāng)前的狀態(tài),判斷是該橫屏還是豎屏!

- (void)changeScreenAction{
    SEL selector=NSSelectorFromString(@"setOrientation:");
    NSInvocation *invocation =[NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
    [invocation setSelector:selector];
    [invocation setTarget:[UIDevice currentDevice]];
    int val = _isFullScreen?UIInterfaceOrientationLandscapeRight:UIInterfaceOrientationPortrait;
    [invocation setArgument:&val atIndex:2];
    [invocation invoke];
}
解析:
  1. 找到setOrientation:方法對(duì)應(yīng)的SEL類型的數(shù)據(jù),我用了一個(gè)局部變量selector暫存起來(lái)
   SEL selector=NSSelectorFromString(@"setOrientation:");

2.NSInvocation 是實(shí)現(xiàn)命令模式的一種,可以調(diào)取任意的SEL或block。當(dāng)NSInvocation被調(diào)用,它會(huì)在運(yùn)行時(shí),通過(guò)目標(biāo)對(duì)象去尋找對(duì)應(yīng)的方法,從而確保唯一性。
NSInvocation創(chuàng)建方法

+ (NSInvocation *)invocationWithMethodSignature:(NSMethodSignature *)sig;

NSMethodSignature是一個(gè)方法簽名的類,通常使用下面的方法,獲取對(duì)應(yīng)方法的簽名

[消息接受者 instanceMethodSignatureForSelector:selector];

eg:

NSInvocation *invocation =[NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];

3.設(shè)置方法:

[invocation setSelector:selector];

4.設(shè)置執(zhí)行方法的對(duì)象

[invocation setTarget:[UIDevice currentDevice]];

5.判斷當(dāng)前的狀態(tài)是橫屏還是豎屏。利用三目運(yùn)算符,得到UIInterfaceOrientationLandscapeRight(橫屏)或UIInterfaceOrientationPortrait(豎屏),得到的結(jié)果其實(shí)是一個(gè)枚舉,如下:

typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
}

對(duì)應(yīng)的代碼如下:

int val = _isFullScreen?UIInterfaceOrientationLandscapeRight:UIInterfaceOrientationPortrait;

6.設(shè)置執(zhí)行方法的參數(shù)

- (void)setArgument:(void *)argumentLocation atIndex:(NSInteger)idx;

argumentLocation傳遞的是參數(shù)的地址。index 從2開(kāi)始,因?yàn)? 和 1 分別為 target 和 selector。
7.調(diào)用這個(gè)方法

[invocation invoke];

參考:NSInvocation 的使用之——強(qiáng)制屏幕旋轉(zhuǎn)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)至元數(shù)據(jù)結(jié)尾創(chuàng)建: 董瀟偉,最新修改于: 十二月 23, 2016 轉(zhuǎn)至元數(shù)據(jù)起始第一章:isa和Class一....
    40c0490e5268閱讀 1,767評(píng)論 0 9
  • 目錄 Objective-C Runtime到底是什么 Objective-C的元素認(rèn)知 Runtime詳解 應(yīng)用...
    Ryan___閱讀 1,950評(píng)論 1 3
  • 1.監(jiān)聽(tīng)屏幕旋轉(zhuǎn)方向 在處理iOS橫豎屏?xí)r,經(jīng)常會(huì)和UIDeviceOrientation、UIInterface...
    彬至睢陽(yáng)閱讀 2,548評(píng)論 1 6
  • 1,NSObject中description屬性的意義,它可以重寫(xiě)嗎?答案:每當(dāng) NSLog(@"")函數(shù)中出現(xiàn) ...
    eightzg閱讀 4,185評(píng)論 2 19
  • 2016-5-26 今天晚上8點(diǎn)多的時(shí)候,不甘心的我,再次查看了我的鎖狀態(tài),吃驚的發(fā)現(xiàn)已經(jīng)變成了“關(guān)閉”狀態(tài)。讓我...
    峰眼看世界閱讀 148評(píng)論 0 2