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

这一节来把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广播的基本使用。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值