Skip to content

FullScreenable

林洵锋 edited this page Sep 15, 2018 · 10 revisions

一、配置

  • 若项目支持横屏(Landscape)则跳过此配置步骤
  • 若项目只支持竖屏(Portrait)的话需要在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时,如果specifiedViewnil会自动填写,superView也一样

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

lxf_FullScreenable_1

以下分两种情况说明

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()
view.lxf.switchFullScreen(isEnter: !cyanView.lxf.isFullScreen)

2、进入全屏

cyanView.lxf.enterFullScreen()

3、退出全屏

cyanView.lxf.exitFullScreen()

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

lxf_FullScreenable_2

Clone this wiki locally