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

如果对面向协议有疑问的同学可以看下我之前的两篇文章

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

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

+

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

如果对面向协议有疑问的同学可以看下我之前的两篇文章

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

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

开源库

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

使用Cocoapods的方式来安装即可

1
pod 'LXFProtocolTool/FullScreenable'

一、配置

在AppDelegate中实现如下方法

1
2
3
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return UIApplication.shared.lxf.currentVcOrientationMask
}

二、使用案例

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

1
isFullScreen : 获取当前遵守协议者是否为全屏状态
1
2
3
4
5
6
7
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也是如此

switchFullScreen方法不推荐直接使用,不过当遵守协议者为UIViewController时,可以通过使用默认参数来切换屏幕方向lxf.switchFullScreen()

lxf_FullScreenable_1

以下分两种情况说明

UIViewController

1
2
3
4
5
func enterFullScreen(
specifiedView: UIView,
config: FullScreenableConfig? = nil,
completed: FullScreenableCompleteType? = nil
)
1
2
3
4
5
func exitFullScreen(
superView: UIView,
config: FullScreenableConfig? = nil,
completed: FullScreenableCompleteType? = nil
)

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

1、遵守协议 FullScreenable

1
class LXFFullScreenableController: UIViewController, FullScreenable { }

2、指定视图进入全屏

1
lxf.enterFullScreen(specifiedView: cyanView)

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

1
lxf.exitFullScreen(superView: self.view)

🔥自动进入|退出全屏

1
2
3
4
5
func autoFullScreen(
specifiedView: UIView,
superView: UIView,
config: FullScreenableConfig? = nil
)
  • 控制器可以调用该方法来注册自动进入或退出全屏,各控制器之间互不影响。
  • view手动进入全屏会屏蔽当前控制器的自动全屏功能,退出方可恢复

UIView

1
2
3
4
5
func enterFullScreen(
specifiedView: UIView? = nil,
config: FullScreenableConfig? = nil,
completed: FullScreenableCompleteType? = nil
)
1
2
3
4
5
func exitFullScreen(
superView: UIView? = nil,
config: FullScreenableConfig? = nil,
completed: FullScreenableCompleteType? = nil
)

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

1、遵守协议 FullScreenable

1
class LXFFullScreenView: UIButton, FullScreenable { }
1
let cyanView = LXFFullScreenView()

2、进入全屏

1
cyanView.lxf.enterFullScreen()

3、退出全屏

1
cyanView.lxf.exitFullScreen()

这里是对遵守了FullScreenable协议的视图进入全屏切换,由于代码内部已经经过自动视图填写,所以直接调用相应的方法即可,当然也可以自己指定specifiedViewsuperView

lxf_FullScreenable_2

三、FullScreenableConfig说明

上述的方法都有一个config参数,默认为nil,即为默认配置

相关属性说明
| Name | Type | Desc | Default |
| ————————– | ———————— | —————————— | ————– |
| animateDuration | Double | 进入/退出 全屏时的旋转动画时间 | 0.25 |
| enterFullScreenOrientation | UIInterfaceOrientation | 进入全屏时的初始方向 | landscapeRight |

这里我们把动画时间设置为1s,初始方向为后来看看效果

1
2
3
4
FullScreenableConfig(
animateDuration: 1,
enterFullScreenOrientation : .landscapeLeft
)

1
2
cyanView.lxf.enterFullScreen(config: diyConfig)
cyanView.lxf.exitFullScreen(config: diyConfig)

lxf_FullScreenable_3

结语

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

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