SwiftTheme--iOS换肤解决方案

https://www.jianshu.com/p/6fd1324cfbae

参考Github文献:https://github.com/jiecao-fm/SwiftTheme/blob/master/README_CN.md

前言

缘起
项目需求,我们要为节操精选开发夜间模式功能。我们的需求不是简单的调整亮度或者alpha,而是更换为一套更深色的UI。因此所谓夜间模式其实就是特定的更换主题(换肤)功能。
如何实现呢?判断某个全局变量,然后在初始化视图控件时设置不同的背景色或者加载不同的切图文件?但是在切换主题时,已经初始化好的视图控件呢?没错,也许你也想到了通过通知让相应的视图控件修改背景色或切图。想到这里你应该也意识到了Controller中将充斥着注册通知、if...else、更新视图控件的代码,糟糕的是如果忘记了注销通知还可能引起应用崩溃。
一番思考后,我们对该任务提出了更高的要求,打造一套简单、可复用的主题框架,正如你看到的这样。

目标
将SwiftTheme打造为一款简单、功能丰富、高性能、可扩展的主题框架(换肤框架),为iOS 平台提供一个统一的主题解决方案。

示例
索引方式
让 UIView 随主题变换背景色:
view.theme_backgroundColor = ["#FFF", "#000"]

让 UILabel 和 UIButton 随主题变换文字颜色:
label.theme_textColor = ["#000", "#FFF"]
button.theme_setTitleColor(["#000", "#FFF"], forState: .Normal)

让 UIImageView 随主题变换切图:
imageView.theme_image = ["day", "night"]

// 不想通过切图名,想通过 UIImage 来设置不同主题的图片也是可以的
imageView.theme_image = ThemeImagePicker(images: image1, image2)
然后,当你执行如下代码时,奇迹发生了!

// 例如isNight为true,imageView将会使用 "night" 的切图
ThemeManager.setTheme(index: isNight ? 1 : 0)



作者:路漫漫其修远兮Wzt
链接:https://www.jianshu.com/p/6fd1324cfbae
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
 

Swift4.x普通笔记06 SwiftTheme主题换肤,Swift广播的基本使用,X的适配

https://blog.csdn.net/v2810769/article/details/84725480

2018年12月03日 15:09:22 梦中一夜下江南 阅读数:117

 

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/v2810769/article/details/84725480

这一节来把SwiftTheme搞明白怎么使用,它是一个主题换肤的框架。下面开始使用

想说一个它的问题,不能跨控制器使用,如果要用的话就要用到广播。这个我们稍后会讲。

 

 

 

X的适配提前讲了,812是它的高度

X的底部高度改成49就可以了。猫耳朵改高一些就好了。

 

 

 

首先  pod 'SwiftTheme'   后面不加版本代表使用最新版本

pod update   (时间多的话,用这条)

pod update --verbose --no-repo-update  (时间不多的话,用这条)

然后command + B   编译一下

这样我们就安装好了

 

接下来你会想怎么使用,接下来是我实现过的流程。我们写一个枚举类MyTheme,你如果用,直接复制。

import Foundation

import SwiftTheme

enum MyTheme:Int {

    白天的标识

    case day = 0

    黑夜的标识

    case night = 1

    定义之前的标识

    static var before = MyTheme.day

    定义现在的标识

    static var current = MyTheme.night

    用于切换主题,主要的类ThemeManager,根据plist来使用

    static func switchTo(_ theme:MyTheme){

        这边做一个替换

        before = current

        current = theme

        这边设置主题

        switch theme {

            case .day:ThemeManager.setTheme(plistName: "default_theme", path: .mainBundle)

            case .night:ThemeManager.setTheme(plistName: "night_theme", path: .mainBundle)

        }

    }

    这边选择设置主题

    static func switchNight(_ isToNight:Bool) {

        switchTo(isToNight ? .night:.day)

    }

    static func isNight() -> Bool{

        return current == .night

    }

}

接下来的步骤是定义两个plist文件,你有几个主题定义几个plist文件

 

--------------------------------------------------------------------------------------------------------------------------------------------

default_theme.plist

图片分一组

.......

images : 

            likeButtonbg  :  "like_btn_24*24"

颜色分一组

........

colors:

           black: "#000000"

--------------------------------------------------------------------------------------------------------------------------------------------

night_theme.plist

图片分一组

.......

images : 

            likeButtonbg  :  "like_btn_night_24*24"

颜色分一组

........

colors:

           black: "#000000"

--------------------------------------------------------------------------------------------------------------------------------------------

我这里是调用枚举类的静态方法,MyTheme.switchNight(true)

到这里我们就完成了7成,剩下的只要像下面这样调用就好了,如果不懂可以来博客下留言。

        leftLabel.theme_textColor = "colors.black"

        rightLabel.theme_textColor = "colors.cellRightTextColor"

        rightImageView.theme_image = "images.cellRightArrow"

        separatorView.theme_backgroundColor = "colors.separatorColor"

        theme_backgroundColor = "colors.cellBackgroundColor"


        .....这里换成你对应的控件.....

        leftLabel.theme_textColor = "colors.black"

        rightLabel.theme_textColor = "colors.cellRightTextColor"

        rightImageView.theme_image = "images.cellRightArrow"

        theme_backgroundColor = "colors.cellBackgroundColor"

        topView.theme_backgroundColor =  "colors.cellBackgroundColor"

        collectionView.theme_backgroundColor = "colors.cellBackgroundColor"

        ...................

 

这样我们的换肤就好了,我利用的是按钮的点击,选中为夜晚,不选中为白天。

 

接下来,我们实现一个功能,就是当我关闭App时,再次打开时,还是会显示上次保存的主题。

在我们的按钮的点击事件里面,实现状态的保存。以下是代码。

    @IBAction func dayOrNightButtonClicked(_ sender: UIButton) {

        sender.isSelected = !sender.isSelected

        这一行,就可以实现状态的保存

        UserDefaults.standard.set(sender.isSelected, forKey: "isNight")

    }

读取状态,然后进行设置。我在AppDelegate.swift进行设置。



 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        //根据isNight,读取主题

        ThemeManager.setTheme(plistName: UserDefaults.standard.bool(forKey: isNight) ? "night_theme":"default_theme", path: .mainBundle)

        .....................



    }

接下来是控制器之间的通知使用方法

1,在我们切换主题的按钮里面使用NotificationCenter,,,发送


    @IBAction func dayOrNightButtonClicked(_ sender: UIButton) {

        sender.isSelected = !sender.isSelected

        .................

        这里的dayOrNightButtonCLicked只是作为标识,

        sender.isSelected这是要传递过去的变量。

        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "dayOrNightButtonCLicked"), object: sender.isSelected)



    }

2,,,在目录控制器里面接收

    override func viewDidLoad() {

        super.viewDidLoad()

        ..............

        这个是回调的方法。

        receiveDayOrNightButtonClicked

        NotificationCenter.default.addObserver(self, selector: #selector(receiveDayOrNightButtonClicked), name: NSNotification.Name(rawValue: "dayOrNightButtonCLicked"), object: nil)

    }

下面我们来定义这个receiveDayOrNightButtonClicked

    @objc func receiveDayOrNightButtonClicked(notification:Notification) {

        这里我们得到了我们的对象

        let selected = notification.object as! Bool

        ...........接下类就是你的逻辑了

 

    }

记住通知用完要进行销毁,如果在控制器里面不销毁的话,可能会崩溃。

    deinit {

        销毁通知

        NotificationCenter.default.removeObserver(self)

    }

 

没讲太多东西,就两个,SwiftTheme主题换肤,Swift广播的基本使用。

 

 

 

 

 

posted @ 2019-04-03 09:29  sundayswift  阅读(1444)  评论(0编辑  收藏  举报