iOS實(shí)現(xiàn)手動(dòng)和自動(dòng)屏幕旋轉(zhuǎn)
本文實(shí)例為大家分享了iOS實(shí)現(xiàn)手動(dòng)和自動(dòng)屏幕旋轉(zhuǎn)的具體代碼,供大家參考,具體內(nèi)容如下
首先iPhone中屏幕分為狀態(tài)欄方向和設(shè)備方向
typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
? ? UIDeviceOrientationUnknown,
? ? UIDeviceOrientationPortrait, ? ? ? ? ? ?// Device oriented vertically, home button on the bottom
? ? UIDeviceOrientationPortraitUpsideDown, ?// Device oriented vertically, home button on the top
? ? UIDeviceOrientationLandscapeLeft, ? ? ? // Device oriented horizontally, home button on the right
? ? UIDeviceOrientationLandscapeRight, ? ? ?// Device oriented horizontally, home button on the left
? ? UIDeviceOrientationFaceUp, ? ? ? ? ? ? ?// Device oriented flat, face up
? ? UIDeviceOrientationFaceDown ? ? ? ? ? ? // Device oriented flat, face down
};
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
? ? UIInterfaceOrientationUnknown ? ? ? ? ? ?= UIDeviceOrientationUnknown,
? ? UIInterfaceOrientationPortrait ? ? ? ? ? = UIDeviceOrientationPortrait,
? ? UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
? ? UIInterfaceOrientationLandscapeLeft ? ? ?= UIDeviceOrientationLandscapeRight,
? ? UIInterfaceOrientationLandscapeRight ? ? = UIDeviceOrientationLandscapeLeft
};系統(tǒng)提供兩個(gè)地方來(lái)設(shè)置設(shè)備的方向,取兩個(gè)地方的交集是最后的設(shè)備所支持的方向
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window; -(NSUInteger)supportedInterfaceOrientations;
這里需要注意的是返回的時(shí)下面的枚舉
typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
? ? UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
? ? UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
? ? UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
? ? UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
? ? UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
? ? UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
? ? UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
};在轉(zhuǎn)動(dòng)屏幕的時(shí)候會(huì)觸發(fā)下面方法
-(BOOL)shouldAutorotate;
在該方法返回真,自動(dòng)調(diào)用上面的兩個(gè)方法得到方向。
修改狀態(tài)欄方向的方法
1、使用私有API setOrientation;
2、修改狀態(tài)欄的方向,并通過(guò)設(shè)置View的transform來(lái)達(dá)到偽旋轉(zhuǎn)的結(jié)果,但是設(shè)備方向并沒(méi)有改變
3、主動(dòng)出發(fā)系統(tǒng)支持的方法,就相當(dāng)于讓這個(gè)vc在重新出來(lái)的時(shí)候系統(tǒng)判斷所支持的方向的機(jī)制重新走一遍。
- (void)awakeSupportInterOrtation:(UIViewController *)showVC completion:(void(^)(void))block
{
? ? UIViewController *vc = [[UIViewController alloc] init];
? ? void(^completion)() = ^() {
? ? ? ? [showVC dismissViewControllerAnimated:NO completion:^{
? ? ? ? ? ? if (block)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? block();
? ? ? ? ? ? }
? ? ? ? }];
? ? };
? ? // This check is needed if you need to support iOS version older than 7.0
? ? BOOL canUseTransitionCoordinator = [showVC respondsToSelector:@selector(transitionCoordinator)];
? ? if (canUseTransitionCoordinator)
? ? {
? ? ? ? [showVC presentViewController:vc animated:NO completion:nil];
? ? ? ? [showVC.transitionCoordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
? ? ? ? ? ? completion();
? ? ? ? }];
? ? }
? ? else
? ? {
? ? ? ? [showVC presentViewController:vc animated:NO completion:completion];
? ? }
}
-(NSUInteger)supportedInterfaceOrientations
{
? ? ? ? return UIInterfaceOrientationMaskPortrait;
}
-(BOOL)shouldAutorotate
{
? ? return YES;
}在需要轉(zhuǎn)為豎屏的時(shí)候調(diào)用一個(gè)方法,在后面兩個(gè)方法中如上實(shí)現(xiàn),第二個(gè)方法中返回的是你最終要轉(zhuǎn)向的方向。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS中設(shè)置清除緩存功能的實(shí)現(xiàn)方法
清除緩存基本上都是在設(shè)置界面的某一個(gè)Cell,于是我們可以把清除緩存封裝在某一個(gè)自定義Cell中,現(xiàn)在位大家介紹一種最基礎(chǔ)的清除緩存的方法,感興趣的朋友一起看看吧2017-07-07
iOS中實(shí)現(xiàn)imageView任意角度旋轉(zhuǎn)的方法
這篇文章主要給大家介紹了關(guān)于iOS中實(shí)現(xiàn)imageView任意角度旋轉(zhuǎn)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
iOS開(kāi)發(fā)探索多線程GCD常用函數(shù)
這篇文章主要為大家介紹了iOS開(kāi)發(fā)探索多線程GCD常用函數(shù)的使用示例介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
iOS 基于AFNetworking下自簽名證書(shū)配置的方法
本篇文章主要介紹了iOS 基于AFNetworking下自簽名證書(shū)配置的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
IOS應(yīng)用內(nèi)跳轉(zhuǎn)系統(tǒng)設(shè)置相關(guān)界面的方法
在iOS開(kāi)發(fā)中,有時(shí)會(huì)有跳轉(zhuǎn)系統(tǒng)設(shè)置界面的需求,例如提示用戶(hù)打開(kāi)藍(lán)牙或者WIFI,提醒用戶(hù)打開(kāi)推送或者位置權(quán)限等,接下來(lái)通過(guò)本文給大家介紹IOS應(yīng)用內(nèi)跳轉(zhuǎn)系統(tǒng)設(shè)置相關(guān)界面的方法,喜歡的朋友參考下2016-02-02
UIPageViewController實(shí)現(xiàn)的左右滑動(dòng)界面
這篇文章主要為大家詳細(xì)介紹了UIPageViewController實(shí)現(xiàn)的左右滑動(dòng)界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
ios scrollview嵌套tableview同向滑動(dòng)的示例
本篇文章主要介紹了ios scrollview嵌套tableview同向滑動(dòng)的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
iOS App項(xiàng)目中引入SQLite數(shù)據(jù)庫(kù)的教程
SQLite是一個(gè)極輕的嵌入式數(shù)據(jù)庫(kù),在應(yīng)用程序中捆綁使用可以更方便地幫助操控關(guān)系型數(shù)據(jù),這里我們就來(lái)看一下iOS App項(xiàng)目中引入SQLite數(shù)據(jù)庫(kù)的教程2016-06-06
在iOS開(kāi)發(fā)的Quartz2D使用中實(shí)現(xiàn)圖片剪切和截屏功能
這篇文章主要介紹了在iOS開(kāi)發(fā)的Quartz2D使用中實(shí)現(xiàn)圖片剪切和截屏功能的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12
基于iOS Realm數(shù)據(jù)庫(kù)的使用實(shí)例詳解
下面小編就為大家分享一篇基于iOS Realm數(shù)據(jù)庫(kù)的使用實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01

