降低项目耦合性之换肤功能的解析封装

<h1>我们要先知道</h1>
<h4>1.皮肤是什么? 图片
2.换肤是什么 ? 改变图片
3.通过什么方式改变? 管理类加上通知的方法
4.改变那些图片?tabbar,navigation,我的,以及页面的背景。
5.换肤应用在那里? (1)节假日切换主题(2)切换夜晚模式(3)大主题
6.必要条件是什么? (1)不同主题的同一位置图片名称必须相同(2) 我们的所有controller都继承于一个BaseViewController。BaseViewController里的方法</h4>(这里只以navigation和tarbar人为例了。)
<pre>

  • (void) reloadThemeImage {
    ThemeManager * themeManager = [ThemeManager sharedThemeManager];

    UIImage * navigationBackgroundImage = [themeManager themeImageWithName:@"navigationbar_background.png"];
    [self.navigationController.navigationBar setBackgroundImage:navigationBackgroundImage forBarMetrics:UIBarMetricsDefault];

    UIImage * tabBarBackgroundImage = [themeManager themeImageWithName:@"tabbar_background.png"];
    [self.tabBarController.tabBar setBackgroundImage:tabBarBackgroundImage];
    }
    </pre>
    在初始化BaseViewController的方法里加上通知
    <pre>

  • (id) init {
    if (self == [super init]) {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeChangedNotfication:) name:@"通知名" object:nil];
    }

    [self reloadThemeImage];
    return self;
    }
    //通知方法

  • (void) themeChangedNotfication:(NSNotification *)notification {
    [self reloadThemeImage];
    }
    </pre>

点击换主题地方代码
<pre>

  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    ThemeManager * themeManager = [ThemeManager sharedThemeManager];
    NSString * themeName = self.themeDataSource[indexPath.row];

    if ([themeName isEqualToString:@"默认"]) {
    themeName = nil;
    }
    // 记录当前主题名字
    themeManager.themeName = themeName;
    [[NSNotificationCenter defaultCenter] postNotificationName:kThemeChangedNotification object:nil];

// 主题持久化
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:themeName forKey:kThemeNameKey];
[userDefaults synchronize];

// 重新加载数据显示UITableViewCellAccessoryCheckmark 显示选中的对号 v
[self.tableView reloadData];

}
</pre>
<h2>管理类</h2>

.h文件
<pre>

import <Foundation/Foundation.h>

import <UIKit/UIKit.h>

@interface ThemeManager : NSObject

@property (nonatomic, copy) NSString * themeName; // 主题名字
@property (nonatomic, retain) NSDictionary * themePlistDict; // 主题属性列表字典

  • (ThemeManager *) sharedThemeManager;
  • (UIImage *) themeImageWithName:(NSString *)imageName;
    @end
    </pre>
    .m文件
    <pre>

import "ThemeManager.h"

import "NotificationMacro.h"

static ThemeManager * sharedThemeManager;
@implementation ThemeManager

  • (id) init {
    if(self = [super init ]) {
    NSString * themePath = [[NSBundle mainBundle] pathForResource:@"theme" ofType:@"plist"];
    self.themePlistDict = [NSDictionary dictionaryWithContentsOfFile:themePath];
    self.themeName = nil;
    }
    return self;
    }
  • (ThemeManager *) sharedThemeManager {
    @synchronized(self) {
    if (nil == sharedThemeManager) {
    sharedThemeManager = [[ThemeManager alloc] init];
    }

    return sharedThemeManager;
    }

// Override 重写themeName的set方法

  • (void) setThemeName:(NSString *)themeName {
    _themeName = themeName;
    }

  • (UIImage *) themeImageWithName:(NSString *)imageName {
    if (imageName == nil) {
    return nil;
    }

    NSString * themePath = [self themePath];
    NSString * themeImagePath = [themePath stringByAppendingPathComponent:imageName];
    UIImage * themeImage = [UIImage imageWithContentsOfFile:themeImagePath];

    return themeImage;
    }

// 返回主题路径

  • (NSString *)themePath {
    NSString * resourcePath = [[NSBundle mainBundle] resourcePath];
    if (self.themeName == nil || [self.themeName isEqualToString:@""]) {
    return resourcePath;
    }
    NSString * themeSubPath = [self.themePlistDict objectForKey:self.themeName]; // Skins/blue
    NSString * themeFilePath = [resourcePath stringByAppendingPathComponent:themeSubPath]; // .../Skins/blue
    return themeFilePath;
    }
    @end
    </pre>
    theme.plist是什么
2F58A9D3-F16E-4BBA-B12C-FA079B7E4519.png

资源目录结构


64229767-8F68-45A9-A948-47928B74F105.png

源码地址

其他背景统一封装在baseviewcontroller中用。无法封装的背景只能另接收通知处理。当然是肯定可以封装的。hiden方法控制在唯一界面显示。有更好的方法欢迎指点。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 7,347评论 1 14
  • 技术无极限,从菜鸟开始,从源码开始。 由于公司目前项目还是用OC写的项目,没有升级swift 所以暂时SDWebI...
    充满活力的早晨阅读 14,338评论 0 2
  • 1、禁止手机睡眠 [UIApplication sharedApplication].idleTimerDisab...
    FF_911阅读 5,248评论 0 3
  • assert($a)检查$a是否为false,是返回true getenv($str)获取一个环境变量的值 php...
    iscona阅读 1,760评论 0 1
  • 随着科学技术的发展,人们生活方式的改变,现代教育的形式也变得多元化。老师以前都是亲手批改学生的作业,现在有一种新型...
    王栎涵阅读 4,181评论 2 1

友情链接更多精彩内容