美文网首页小知识点iOS11iOS进阶之路
iOS 面向协议封装全屏旋转功能

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

作者: LinXunFeng | 来源:发表于2018-09-15 17:46 被阅读228次

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

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

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

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

    开源库

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

    使用Cocoapods的方式来安装即可

    pod 'LXFProtocolTool/FullScreenable'
    

    一、配置

    • 若项目支持横屏(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()
    

    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

    结语

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

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

    相关文章

      网友评论

        本文标题:iOS 面向协议封装全屏旋转功能

        本文链接:https://www.haomeiwen.com/subject/avhtnftx.html