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

首頁 > 系統 > iOS > 正文

總結iOS App開發中控制屏幕旋轉的方式

2020-02-19 15:50:04
字體:
來源:轉載
供稿:網友

我們在開發項目的時候,都希望當屏幕旋轉時,界面的布局會相應地改變而得以適配新的布局,有需要的朋友們可以跟著武林技術頻道小編的步伐好好參考一番!

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

?

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


程序將使用如下2個方法來代替:

?

?

?


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


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

?

201622991714779.png (493×56)

2.

201622991742247.png (492×242)

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

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

?

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];?
}?


如果需要設置viewCtrl的旋轉,那么不能在UIViewController里面重寫shouldAutorotate和supportedInterfaceOrientations方法,而是需要在navCtrl里面設置,又因為UINavigationController是系統控件,所以這里需要新建一個UINavigationController的子navigationController的子類,然后在里面實現shouldAutorotate和supportedInterfaceOrientations方法,比如:

?

?

?


-(NSUInteger)supportedInterfaceOrientations{?
??? return UIInterfaceOrientationMaskAllButUpsideDown;?
}?
?
- (BOOL)shouldAutorotate{?
??? return YES;?
}


eg1:如果上面的例子是self.window.rootViewController = viewCtrl,而不是navCtrl,那么上面的那2個控制旋轉的方法就應該寫在UIViewController里面!

?

eg2:如果viewCtrl又pushViewController到viewCtrl2,需要設置viewCtrl2的旋轉,怎么辦呢? 還是在navCtrl里面控制,因為viewCtrl和viewCtrl2的rootViewController都是navCtrl,一般的寫法都是

?

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


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

?

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

?

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


全部調用self.topViewController,就是返回當前呈現出來的viewController里面的設置,然后在viewCtrl、viewCtrl2等等這些viewController里面重寫shouldAutorotate和supportedInterfaceOrientations,以方便設置每個viewController的旋轉

?

eg3:如果viewCtrl 是 presentModalViewController 到 viewCtrl3,那么viewCtrl3的旋轉設置就不在navCtrl里面了!如果presentModalViewController的viewController是navController、tabbarController包裝過的viewCtrl3,那么就應在新包裝的navController、tabbarController里面設置,如果是直接presentModalViewController到viewCtrl3,那么就在viewCtrl3里面設置


手動旋轉
手動旋轉也有2種方式,一種是直接設置 UIDevice 的 orientation,但是這種方式不推薦,上傳appStore有被拒的風險:

?

if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {?
??? [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIInterfaceOrientationPortrait];?
}

?
第二種是假旋轉,并沒有改變 UIDevice 的 orientation,而是改變某個view的 transform,利用 CGAffineTransformMakeRotation 來達到目的,比如:

?

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


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

?

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

?

- (BOOL)shouldAutorotate{?
??????? return NO;?
}

?
手動觸發某個按鈕,調用方法,這個方法的實現如下:

?

[[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都會跟著自動變;其次因為方向變了,所以self.view的大小需要重新設置,不要使用self.view.frame,而是用bounds。
2. 如果shouldAutorotate 返回YES的話,下面設置setStatusBarOrientation 是不管用的!setStatusBarOrientation只有在shouldAutorotate 返回NO的情況下才管用!

?

強制旋轉屏幕
最近接手了一個項目,正常情況下使用查看圖片是沒問題的。

用到了 MWPhotoBrowser 這個第三方圖片瀏覽庫。
不過發現了一個問題,就是設備橫屏modal這MWPhotoBrowser的時候,發生了圖片位置錯亂。

實在沒辦法,所以想到了一個餿主意。

就是modal的時候使用代碼把設備強制旋轉回去。

?

//UIDevice+WJ.h
@interface UIDevice (WJ)
/**
?*? 強制旋轉設備
?*? @param? 旋轉方向
?*/
+ (void)setOrientation:(UIInterfaceOrientation)orientation;
@end
//UIDevice+WJ.m
#import "UIDevice+WJ.h"
@implementation UIDevice (WJ)
//調用私有方法實現
+ (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

以上就是武林技術頻道為各位朋友們介紹的總結iOS App開發中控制屏幕旋轉的方式,大家可以在上文中獲得非常全面的信息,希望上面的內容,可以為大家提供幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 汾西县| 鄂伦春自治旗| 无棣县| 九寨沟县| 当雄县| 南城县| 石阡县| 庄浪县| 岚皋县| 确山县| 仪征市| 上栗县| 游戏| 旬阳县| 当雄县| 曲周县| 长春市| 五大连池市| 巴林左旗| 绍兴市| 榆树市| 公安县| 青龙| 桐梓县| 乐亭县| 全州县| 镇原县| 当雄县| 德江县| 江川县| 锦屏县| 太保市| 宁南县| 淮北市| 安达市| 昌江| 七台河市| 张掖市| 河南省| 盐边县| 利津县|