国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

總結(jié)iOS App開發(fā)中控制屏幕旋轉(zhuǎn)的幾種方式

2020-07-26 03:27:00
字體:
供稿:網(wǎng)友

在iOS6之前的版本中,通常使用 shouldAutorotateToInterfaceOrientation 來單獨(dú)控制某個(gè)UIViewController的方向,需要哪個(gè)viewController支持旋轉(zhuǎn),只需要重寫shouldAutorotateToInterfaceOrientation方法。
但是iOS 6里屏幕旋轉(zhuǎn)改變了很多,之前的 shouldAutorotateToInterfaceOrientation 被列為 DEPRECATED 方法,查看UIViewController.h文件也可以看到:

復(fù)制代碼 代碼如下:

// Applications should use supportedInterfaceOrientations and/or shouldAutorotate.. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0); 

程序?qū)⑹褂萌缦?個(gè)方法來代替:
復(fù)制代碼 代碼如下:

- (BOOL)shouldAutorotate; 
- (NSUInteger)supportedInterfaceOrientations; 

除了重寫這個(gè)2個(gè)方法,IOS6之后要旋轉(zhuǎn)還有一些需要注意的地方,下面會細(xì)述。另外還有一個(gè)硬性條件,需要在Info.plist文件里面添加程序支持的所有方向,可以通過以下2種方式添加
1.

201622991714779.png (493×56)

2.

201622991742247.png (492×242)

另外要兼容IOS6之前的系統(tǒng),要保留原來的 shouldAutorotateToInterfaceOrientation 方法,還有那些 willRotateToInterfaceOrientation 等方法。

自動旋轉(zhuǎn)設(shè)置:
控制某個(gè)viewController旋轉(zhuǎn)并不是像IOS5或者IOS4一樣在這個(gè)viewController里面重寫上面那2個(gè)方法,而是需要在這個(gè)viewController的rootViewController(根視圖控制器)里面重寫,怎么解釋呢?就是最前面的那個(gè)viewController,直接跟self.window接觸的那個(gè)controller,比如以下代碼:

復(fù)制代碼 代碼如下:

UIViewController *viewCtrl = [[UIViewController alloc] init]; 
UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:viewCtrl]; 
if ([window respondsToSelector:@selector(setRootViewController:)]) { 
    self.window.rootViewController = navCtrl; 
} else { 
    [self.window addSubview:navCtrl.view]; 


如果需要設(shè)置viewCtrl的旋轉(zhuǎn),那么不能在UIViewController里面重寫shouldAutorotate和supportedInterfaceOrientations方法,而是需要在navCtrl里面設(shè)置,又因?yàn)閁INavigationController是系統(tǒng)控件,所以這里需要新建一個(gè)UINavigationController的子navigationController的子類,然后在里面實(shí)現(xiàn)shouldAutorotate和supportedInterfaceOrientations方法,比如:
復(fù)制代碼 代碼如下:

-(NSUInteger)supportedInterfaceOrientations{ 
    return UIInterfaceOrientationMaskAllButUpsideDown; 

 
- (BOOL)shouldAutorotate{ 
    return YES; 
}

eg1:如果上面的例子是self.window.rootViewController = viewCtrl,而不是navCtrl,那么上面的那2個(gè)控制旋轉(zhuǎn)的方法就應(yīng)該寫在UIViewController里面!

eg2:如果viewCtrl又pushViewController到viewCtrl2,需要設(shè)置viewCtrl2的旋轉(zhuǎn),怎么辦呢? 還是在navCtrl里面控制,因?yàn)関iewCtrl和viewCtrl2的rootViewController都是navCtrl,一般的寫法都是

復(fù)制代碼 代碼如下:

UIViewController *viewCtrl2 = [[UIVewController alloc] init]; 
[self.navigationController.navigationController pushViewController:viewCtrl2 animated:YES];

所以要控制一個(gè)UINavigationController push到的所有viewController的旋轉(zhuǎn),那么就得在navCtrl里面區(qū)分是哪個(gè)viewController,以便對他們一一控制!同樣如果rootViewController是UITabbarController,那么需要在子類化的UITabbarController里面重寫那2個(gè)方法,然后分別控制!

但是有時(shí)候我初始化UINavigationController的時(shí)候,并不知道所有我所有需要push到的viewController,那么這里有一個(gè)通用的方法,就是讓viewController自己來控制自己,首先在navCtrl里面的實(shí)現(xiàn)方法改為以下方式:

復(fù)制代碼 代碼如下:

- (BOOL)shouldAutorotate   
{   
    return self.topViewController.shouldAutorotate;   
}   
   
- (NSUInteger)supportedInterfaceOrientations   
{   
    return self.topViewController.supportedInterfaceOrientations;   


全部調(diào)用self.topViewController,就是返回當(dāng)前呈現(xiàn)出來的viewController里面的設(shè)置,然后在viewCtrl、viewCtrl2等等這些viewController里面重寫shouldAutorotate和supportedInterfaceOrientations,以方便設(shè)置每個(gè)viewController的旋轉(zhuǎn)

eg3:如果viewCtrl 是 presentModalViewController 到 viewCtrl3,那么viewCtrl3的旋轉(zhuǎn)設(shè)置就不在navCtrl里面了!如果presentModalViewController的viewController是navController、tabbarController包裝過的viewCtrl3,那么就應(yīng)在新包裝的navController、tabbarController里面設(shè)置,如果是直接presentModalViewController到viewCtrl3,那么就在viewCtrl3里面設(shè)置


手動旋轉(zhuǎn)
手動旋轉(zhuǎn)也有2種方式,一種是直接設(shè)置 UIDevice 的 orientation,但是這種方式不推薦,上傳appStore有被拒的風(fēng)險(xiǎn):

復(fù)制代碼 代碼如下:

if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) { 
    [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIInterfaceOrientationPortrait]; 
}
 
第二種是假旋轉(zhuǎn),并沒有改變 UIDevice 的 orientation,而是改變某個(gè)view的 transform,利用 CGAffineTransformMakeRotation 來達(dá)到目的,比如:
復(fù)制代碼 代碼如下:

self.view.transform = CGAffineTransformMakeRotation(M_PI/2) 

下面講解采用第二種方式的各版本手動旋轉(zhuǎn):
思想是首先設(shè)置 statusBarOrientation,然后再改變某個(gè)view的方向跟 statusBarOrientation 一致!

那既然是旋轉(zhuǎn),最少也得有2個(gè)方向,那么還是少不了上面說的那個(gè)硬性條件,先在plist里面設(shè)置好所有可能需要旋轉(zhuǎn)的方向。既然是手動旋轉(zhuǎn),那么就要關(guān)閉自動旋轉(zhuǎn):

復(fù)制代碼 代碼如下:

- (BOOL)shouldAutorotate{ 
        return NO; 
}
 
手動觸發(fā)某個(gè)按鈕,調(diào)用方法,這個(gè)方法的實(shí)現(xiàn)如下:
復(fù)制代碼 代碼如下:

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight]; 
self.view.transform = CGAffineTransformMakeRotation(M_PI/2); 
self.view.bounds = CGRectMake(0, 0, kScreenHeight, 320);
 

注意:
1. 只需要改變self.view.transform,那么self.view的所有subview都會跟著自動變;其次因?yàn)榉较蜃兞耍詓elf.view的大小需要重新設(shè)置,不要使用self.view.frame,而是用bounds。
2. 如果shouldAutorotate 返回YES的話,下面設(shè)置setStatusBarOrientation 是不管用的!setStatusBarOrientation只有在shouldAutorotate 返回NO的情況下才管用!

強(qiáng)制旋轉(zhuǎn)屏幕
最近接手了一個(gè)項(xiàng)目,正常情況下使用查看圖片是沒問題的。

用到了 MWPhotoBrowser 這個(gè)第三方圖片瀏覽庫。
不過發(fā)現(xiàn)了一個(gè)問題,就是設(shè)備橫屏modal這MWPhotoBrowser的時(shí)候,發(fā)生了圖片位置錯(cuò)亂。

實(shí)在沒辦法,所以想到了一個(gè)餿主意。

就是modal的時(shí)候使用代碼把設(shè)備強(qiáng)制旋轉(zhuǎn)回去。

復(fù)制代碼 代碼如下:

//UIDevice+WJ.h
@interface UIDevice (WJ)
/**
 *  強(qiáng)制旋轉(zhuǎn)設(shè)備
 *  @param  旋轉(zhuǎn)方向
 */
+ (void)setOrientation:(UIInterfaceOrientation)orientation;
@end
//UIDevice+WJ.m
#import "UIDevice+WJ.h"
@implementation UIDevice (WJ)
//調(diào)用私有方法實(shí)現(xiàn)
+ (void)setOrientation:(UIInterfaceOrientation)orientation {
    SEL selector = NSSelectorFromString(@"setOrientation:");
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self instanceMethodSignatureForSelector:selector]];
    [invocation setSelector:selector];
    [invocation setTarget:[self currentDevice]];
    int val = orientation;
    [invocation setArgument:&val atIndex:2];
    [invocation invoke];
}
@end

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 赣州市| 邵东县| 金坛市| 东乡县| 山西省| 衡水市| 景宁| 顺义区| 博野县| 静乐县| 进贤县| 泰兴市| 禹城市| 桃园县| 金沙县| 宁强县| 吉木萨尔县| 江源县| 民县| 鹤峰县| 哈尔滨市| 平乡县| 蓬安县| 安溪县| 金堂县| 新乡市| 顺平县| 射阳县| 广宗县| 连城县| 通许县| 济南市| 南澳县| 广灵县| 台东县| 哈巴河县| 万山特区| 贡山| 彭阳县| 嘉荫县| 乌拉特前旗|