iOS换肤

作者: 多情刀客无情刀 | 来源:发表于2017-03-21 23:28 被阅读0次

在开发中常常会遇到换肤的情况,如果每次都用一个判断语句去修改图片会相当的麻烦,为了工作需要我进行了深入研究找到一种方法。

1、创建两个bundle来存放资源文件,里面包含plist文件和图片

把所需要的图片放进去,plist文件中写对应的颜色

2、写一个配置文件来集中处理图片和颜色

#import "DDSkin.h"

#define SkinColor @"DDDkinColor"

@implementation DDSkin

static NSString *_color;

+ (void)initialize {

// 开始,从沙盒中取之前存储的皮肤颜色

_color = [[NSUserDefaults standardUserDefaults] objectForKey:SkinColor];

if (_color == nil) { // 之前没有存储,取出为空,默认为蓝色

_color = @"blue";

}

}

/**

*  通过该方法可以设置皮肤的颜色

*

*  @param color 皮肤的颜色

*/

+ (void)setSkinColor:(NSString *)color {

_color = color;

// 将用户设置的皮肤颜色进行保存

[[NSUserDefaults standardUserDefaults] setObject:color forKey:SkinColor];

[[NSUserDefaults standardUserDefaults] synchronize];

}

/**

*  通过该方法,可以返回想要的UIImage

*

*  @param name 图片的名称

*

*  @return 当前皮肤对应的UIImage

*/

+ (UIImage *)ddSkinWithImageName:(NSString *)name {

NSString *path = [[NSBundle mainBundle] pathForResource:_color ofType:@"bundle"];

NSString *imagePath = [path stringByAppendingString:[NSString stringWithFormat:@"/%@",name]];

return [[UIImage alloc] initWithContentsOfFile:imagePath];

}

/**

*  返回当前皮肤的Label背景颜色

*

*  @return 当前的背景颜色

*/

+ (UIColor *)ddSkinWithColorName:(NSString *)name {

// 1.获取文件夹位置

NSString *path = [[NSBundle mainBundle] pathForResource:_color ofType:@"bundle"];

//获取文件位置

NSString *plistPath = [path stringByAppendingString:@"/color.plist"];

//读取plist当中的数据

NSDictionary *colorDict = [NSDictionary dictionaryWithContentsOfFile:plistPath];

//将字典当中的值取出

NSString *bgStr = [colorDict objectForKey:name];

//将颜色的字符串截取

NSArray *bgArray = [bgStr componentsSeparatedByString:@","];

//取出颜色的r,g,b数值

CGFloat red = [bgArray[0] floatValue];

CGFloat green = [bgArray[1] floatValue];

CGFloat blue = [bgArray[2] floatValue];

//返回当前皮肤的颜色

return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0];

}

@end

3、调用的方法

- (void)viewDidLoad {

[super viewDidLoad];

[self.ragBurron addTarget:self action:@selector(ragButtonTouch) forControlEvents:UIControlEventTouchUpInside];

[self.blueButton addTarget:self action:@selector(blueButtonTouch) forControlEvents:UIControlEventTouchUpInside];

self.blackView.backgroundColor = [DDSkin ddSkinWithColorName:@"color"];

self.imageView.image = [DDSkin ddSkinWithImageName:@"xianxian.jpg"];

}

- (void)ragButtonTouch {

[DDSkin setSkinColor:@"rad"];

self.blackView.backgroundColor = [DDSkin ddSkinWithColorName:@"color"];

self.imageView.image = [DDSkin ddSkinWithImageName:@"xianxian.jpg"];

}

- (void)blueButtonTouch {

[DDSkin setSkinColor:@"blue"];

self.blackView.backgroundColor = [DDSkin ddSkinWithColorName:@"color"];

self.imageView.image = [DDSkin ddSkinWithImageName:@"xianxian.jpg"];

}

4、附GitHub上的demo:https://github.com/wuyezhiguhun/DDSkin

相关文章

  • iOS换肤功能的简单处理框架

    iOS换肤功能的简单处理框架 iOS换肤功能的简单处理框架

  • iOS关于换肤和夜间模式的一些思考

    iOS关于换肤和夜间模式的一些思考 iOS关于换肤和夜间模式的一些思考

  • iOSApp换肤(主题换肤、深浅色、自动换肤)

    iOS换肤 - 主题换肤、深浅色、自动换肤 各种情况下的效果,具体请看代码 使用方法: 1、将Lib文件夹下的 X...

  • iOS 换肤

    应用场景 背景图片 背景颜色 注意问题 问题一:1.默认进来没有颜色2.如果用户选中过颜色,需要将选中的颜色进行存...

  • iOS换肤

    在开发中常常会遇到换肤的情况,如果每次都用一个判断语句去修改图片会相当的麻烦,为了工作需要我进行了深入研究找到一种...

  • iOS 13 UI 适配

    使用 QMUITheme 实现换肤并适配 iOS 13 Dark Mode git传送门 web content ...

  • iOS App 换肤方法 - 本地换肤

    说到主题切换,那么久要做到切换主题瞬间,使所有相关的界面都发生变化,这就需要一种机制来将主题切换这是事件跑出来,并...

  • iOS换肤思路

    1、新建文件夹,将所需要切换的资源文件包括主题色调的图片、颜色(可以保存在plist文件中)等全部整理出来放进去,...

  • iOS换肤方案

    换肤方案在一些文艺App上经常见到。为新项目和老项目添加换肤方案查看原文

  • iOS app换肤

    app 换肤 关于app换肤的几种方式 本地换肤 本地换肤,是通过包里面自身已经拥有的图片和颜色配置,对视图以及各...

网友评论

    本文标题:iOS换肤

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