iOS 面向協議封裝全屏旋轉功能
關於使用面向協議來封裝功能的實戰可以參考我上篇文章 ofollow,noindex">【iOS 面向協議方式封裝空白頁功能】 ,這裡就不再贅述,直接進入使用階段吧
如果對面向協議有疑問的同學可以看下我之前的兩篇文章
開源庫
Name | Link |
---|---|
GitHub | LXFProtocolTool |
Wiki | Wiki首頁 |
本文 Demo | LXFFullScreenable |
使用Cocoapods的方式來安裝即可
pod 'LXFProtocolTool/FullScreenable' 複製程式碼
一、配置
AppDelegate
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { if UIApplication.shared.lxf.allowRotation { // 可旋轉螢幕時所支援的方向 return UIInterfaceOrientationMask.landscape } return .portrait } 複製程式碼
二、使用案例
方法與屬性的呼叫都需要名稱空間加上 lxf
,如 isFullScreen
-> lxf.isFullScreen
isFullScreen : 獲取當前遵守協議者是否為全屏狀態 複製程式碼
func switchFullScreen( isEnter: Bool? = nil, specifiedView: UIView? = nil, superView: UIView? = nil, config: FullScreenableConfig? = nil, completed: ((_ isFullScreen: Bool)->Void)? = nil ) 複製程式碼
Name | Type | Desc |
---|---|---|
isEnter | Bool? |
是否進入全屏 |
specifiedView | UIView? |
指定即將全屏的檢視 |
superView | UIView? |
作為退出全屏後specifiedView的父檢視 |
config | FullScreenableConfig? |
配置 |
completed | ((_ isFullScreen: Bool)->Void)? |
進入/退出 全屏後的回撥 |
當 switchFullScreen
的呼叫者為 UIView
時,如果 specifiedView
為 nil
會自動填寫, superView
也是如此
switchFullScreen
方法不推薦直接使用,不過當遵守協議者為 UIViewController
時,可以通過使用預設引數來切換螢幕方向 lxf.switchFullScreen()

以下分兩種情況說明
UIViewController
func enterFullScreen( specifiedView: UIView, config: FullScreenableConfig? = nil, completed: FullScreenableCompleteType? = nil ) 複製程式碼
func exitFullScreen( superView: UIView, config: FullScreenableConfig? = nil, completed: FullScreenableCompleteType? = nil ) 複製程式碼
以上兩個方法是對 switchFullScreen
的抽離,使呼叫時對引數的傳遞更加清晰
1、遵守協議 FullScreenable
class LXFFullScreenableController: UIViewController, FullScreenable { } 複製程式碼
2、指定檢視進入全屏
lxf.enterFullScreen(specifiedView: cyanView) 複製程式碼
3、指定檢視退出全屏,並新增到當前控制器的 view
上
lxf.exitFullScreen(superView: self.view) 複製程式碼
UIView
func enterFullScreen( specifiedView: UIView? = nil, config: FullScreenableConfig? = nil, completed: FullScreenableCompleteType? = nil ) 複製程式碼
func exitFullScreen( superView: UIView? = nil, config: FullScreenableConfig? = nil, completed: FullScreenableCompleteType? = nil ) 複製程式碼
以上兩個方法是對 switchFullScreen
的抽離,使呼叫時對引數的傳遞更加清晰
1、遵守協議 FullScreenable
class LXFFullScreenView: UIButton, FullScreenable { } 複製程式碼
let cyanView = LXFFullScreenView() 複製程式碼
2、進入全屏
cyanView.lxf.enterFullScreen() 複製程式碼
3、退出全屏
cyanView.lxf.exitFullScreen() 複製程式碼
這裡是對遵守了 FullScreenable
協議的檢視進入全屏切換,由於程式碼內部已經經過自動檢視填寫,所以直接呼叫相應的方法即可,當然也可以自己指定 specifiedView
和 superView

三、FullScreenableConfig說明
上述的方法都有一個 config
引數,預設為nil,即為預設配置
相關屬性說明
Name | Type | Desc | Default |
---|---|---|---|
animateDuration | Double |
進入/退出 全屏時的旋轉動畫時間 | 0.25 |
enterFullScreenOrientation | UIInterfaceOrientation |
進入全屏時的初始方向 | landscapeRight |
這裡我們把動畫時間設定為 1s
,初始方向為 左
後來看看效果
FullScreenableConfig( animateDuration: 1, enterFullScreenOrientation : .landscapeLeft ) 複製程式碼
cyanView.lxf.enterFullScreen(config: diyConfig) cyanView.lxf.exitFullScreen(config: diyConfig) 複製程式碼

結語
到這裡相關的說明已羅列完畢,有什麼不清楚的可以下載 Demo 看看,或者在文章下方留言提問
LXFProtocolTool 主要是通過協議的方式來方便快捷地實現一些的實用功能,除了本文提及的全屏旋轉功能外還有其它實用功能的封裝,具體內容可以到 Wiki首頁 查詢。如果你有什麼想實現的功能也可以提出來,喜歡的就給個Star鼓勵下我吧 :rocket: :rocket: :rocket:,感謝支援!