iOS 面向协议封装全屏旋转功能

关于使用面向协议来封装功能的实战能够参考我上篇文章 【iOS 面向协议方式封装空白页功能】,这里就再也不赘述,咱们直接进入使用阶段吧。 本篇文章只有一个目的,那就是只要遵照协议,一行代码随意切换全屏~git

若是对面向协议有疑问的同窗能够看下我以前的两篇文章github

iOS - Swift 面向协议编程(一)编程

iOS - Swift 面向协议编程(二)swift

开源库

Name Link
GitHub LXFProtocolTool
Wiki Wiki首页
本文 Demo LXFFullScreenable

使用Cocoapods的方式来安装便可ruby

pod 'LXFProtocolTool/FullScreenable'
复制代码

1、配置

在AppDelegate中实现以下方法微信

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return UIApplication.shared.lxf.currentVcOrientationMask
}
复制代码

2、使用案例

方法与属性的调用都须要命名空间加上 lxf,如isFullScreen -> lxf.isFullScreenapp

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时,若是specifiedViewnil会自动填写,superView也是如此post

switchFullScreen方法不推荐直接使用,不过当遵照协议者为UIViewController时,能够经过使用默认参数来切换屏幕方向lxf.switchFullScreen()动画

lxf_FullScreenable_1

如下分两种状况说明spa

UIViewController

func enterFullScreen( specifiedView: UIView, config: FullScreenableConfig? = nil, completed: FullScreenableCompleteType? = nil )
复制代码
func exitFullScreen( superView: UIView, config: FullScreenableConfig? = nil, completed: FullScreenableCompleteType? = nil )
复制代码

以上两个方法是对switchFullScreen的抽离,使调用时对参数的传递更加清晰

一、遵照协议 FullScreenable

class LXFFullScreenableController: UIViewController, FullScreenable { }
复制代码

二、指定视图进入全屏

lxf.enterFullScreen(specifiedView: cyanView)
复制代码

三、指定视图退出全屏,并添加到当前控制器的view

lxf.exitFullScreen(superView: self.view)
复制代码

🔥自动进入|退出全屏

func autoFullScreen( specifiedView: UIView, superView: UIView, config: FullScreenableConfig? = nil ) 
复制代码
  • 控制器能够调用该方法来注册自动进入或退出全屏,各控制器之间互不影响。
  • 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的抽离,使调用时对参数的传递更加清晰

一、遵照协议 FullScreenable

class LXFFullScreenView: UIButton, FullScreenable { }
复制代码
let cyanView = LXFFullScreenView()
复制代码

二、进入全屏

cyanView.lxf.enterFullScreen()
复制代码

三、退出全屏

cyanView.lxf.exitFullScreen()
复制代码

这里是对遵照了FullScreenable协议的视图进入全屏切换,因为代码内部已经通过自动视图填写,因此直接调用相应的方法便可,固然也能够本身指定specifiedViewsuperView

lxf_FullScreenable_2

3、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)
复制代码

lxf_FullScreenable_3

结语

到这里相关的说明已罗列完毕,有什么不清楚的能够下载Demo看看,或者在文章下方留言提问

LXFProtocolTool 主要是经过协议的方式来方便快捷地实现一些的实用功能,除了本文说起的全屏旋转功能外还有其它实用功能的封装,具体内容能够到 Wiki首页 查找。若是你有什么想实现的功能也能够提出来,喜欢的就给个Star鼓励下我吧 🚀 🚀 🚀,感谢支持!

微信公众号
相关文章
相关标签/搜索