Skip to content

FullScreenable

林洵锋 edited this page Sep 25, 2022 · 10 revisions

一、配置

  • 若项目支持横屏(Landscape)则跳过此配置步骤
  • 若项目只支持竖屏(Portrait)的话需要在AppDelegate中实现如下方法
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return UIApplication.shared.lxf.currentVcOrientationMask
}

二、使用案例

方法与属性的调用都需要命名空间加上 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时,如果specifiedViewnil会自动填写,superView也是如此

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

lxf_FullScreenable_1

以下分两种情况说明

UIViewController

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 LXFFullScreenableController: UIViewController, FullScreenable { }

2、指定视图进入全屏

lxf.enterFullScreen(specifiedView: cyanView)

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

lxf.exitFullScreen(superView: self.view)

🔥自动进入 | 退出全屏

func autoFullScreen(
    specifiedView: UIView? = nil,
    superView: UIView? = nil,
    config: FullScreenableConfig? = nil
) 
  • 控制器可以调用该方法来注册自动进入或退出全屏
  • view手动进入全屏会屏蔽当前控制器的自动全屏功能,退出方可恢复

注: 在当前控制器下添加了子控制器时,旋转控制权就会变得很混乱,这时候需要声明谁拥有全屏旋转控制权

步骤:

1、在当前控制器的viewWillAppear指定哪个控制器(简称: vcA)拥有控制权

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    // 指定当前控制器拥有控制权
    lxf.becomeFullScreenMaster()
}

2、且在viewWillDisappear中注销vcA的控制权

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    // 注销当前控制器的控制权
    lxf.resignFullScreenMaster()
}

详细用法见: LXFFullScreenableMultiVcController.swift

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协议的视图进入全屏切换,由于代码内部已经经过自动视图填写,所以直接调用相应的方法即可,当然也可以自己指定specifiedViewsuperView

lxf_FullScreenable_2

三、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