一、基本思路
1> 解决方案1,使用颜色作为图片素材的命名关键字
问题1:要保证每套图片的文件名 颜色+ 名称.png的格式比较麻烦
问题2:如果要将某一个图片应用到其他皮肤不方便
2> 解决方案2,利用Bundle,将图片文件保存在不同的Bundle中
问题:平面设计师维护不方便
3> 解决方案3,利用文件夹(蓝色),将图片文件保存在不同文件夹中
好处:便于平面设计师针对不同的文件夹维护图片素材
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *faceImageView;
@property (weak, nonatomic) IBOutlet UIImageView *heartImageView;
@property (weak, nonatomic) IBOutlet UIImageView *rectImageView;
@end
@implementation ViewController
/*
问题一:默认进来没有皮肤颜色
问题二:没有记录用户选中皮肤颜色
问题三:和美工沟通的问题
问题四:多个控制器的换肤
*/
- (void)viewDidLoad {
[super viewDidLoad];
NSString *skinColor = [[NSUserDefaults standardUserDefaults] objectForKey:@"skinColor"];
if (skinColor == nil) {
skinColor = @"blue";
}
[self changeSkinWithSkinColor:skinColor];
}
- (IBAction)changeToBlueSkin {
[self changeSkinWithSkinColor:@"blue"];
}
- (IBAction)changeToRedSkin {
[self changeSkinWithSkinColor:@"red"];
}
- (IBAction)changeToGreenSkin {
[self changeSkinWithSkinColor:@"green"];
}
- (void)changeSkinWithSkinColor:(NSString *)skinColor
{
NSString *faceImageName = [NSString stringWithFormat:@"skin/%@/face", skinColor];
self.faceImageView.image = [UIImage imageNamed:faceImageName];
NSString *heartImageName = [NSString stringWithFormat:@"skin/%@/heart", skinColor];
self.heartImageView.image = [UIImage imageNamed:heartImageName];
NSString *rectImageName = [NSString stringWithFormat:@"skin/%@/rect", skinColor];
self.rectImageView.image = [UIImage imageNamed:rectImageName];
// 记录用户选中的皮肤颜色
[[NSUserDefaults standardUserDefaults] setObject:skinColor forKey:@"skinColor"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
@end
二、换肤工具类
#import <UIKit/UIKit.h>
@interface SkinTool : NSObject
+ (void)setSKinColor:(NSString *)skinColor;
+ (UIImage *)skinToolWithImageName:(NSString *)imageName;
+ (UIColor *)skinToolWithLabelColor;
@end
#import "SkinTool.h"
@implementation SkinTool
static NSString *_skinColor;
+ (void)initialize
{
_skinColor = [[NSUserDefaults standardUserDefaults] objectForKey:@"skinColor"];
if (_skinColor == nil) {
_skinColor = @"blue";
}
}
+ (void)setSKinColor:(NSString *)skinColor
{
_skinColor = skinColor;
// 保存用户选中的皮肤颜色
[[NSUserDefaults standardUserDefaults] setObject:skinColor forKey:@"skinColor"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
+ (UIImage *)skinToolWithImageName:(NSString *)imageName
{
NSString *imagePath = [NSString stringWithFormat:@"skin/%@/%@", _skinColor ,imageName];
return [UIImage imageNamed:imagePath];
}
+ (UIColor *)skinToolWithLabelColor
{
// 1.获取plist的路径
NSString *plistName = [NSString stringWithFormat:@"skin/%@/bgColor.plist", _skinColor];
NSString *plistPath = [[NSBundle mainBundle] pathForResource:plistName ofType:nil];
// 2.读取颜色的点击
NSDictionary *colorDict = [NSDictionary dictionaryWithContentsOfFile:plistPath];
// 3.读取对应颜色的字符串
NSString *colorString = colorDict[@"labelBgColor"];
// 4.获取颜色数组
NSArray *colorArray = [colorString componentsSeparatedByString:@","];
// 5.读取对应RGB
NSInteger red = [colorArray[0] integerValue];
NSInteger green = [colorArray[1] integerValue];
NSInteger blue = [colorArray[2] integerValue];
return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0];
}
@end
#import "ViewController.h"
#import "SkinTool.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *faceImageView;
@property (weak, nonatomic) IBOutlet UIImageView *heartImageView;
@property (weak, nonatomic) IBOutlet UIImageView *rectImageView;
@end
@implementation ViewController
/*
问题一:默认进来没有皮肤颜色
问题二:没有记录用户选中皮肤颜色
问题三:和美工沟通的问题
问题四:多个控制器的换肤
问题五:换肤的ImageView不要写在viewDidLoad方法
*/
- (void)viewDidLoad {
[super viewDidLoad];
[self changeImages];
}
- (IBAction)changeToOrangeSkin {
[SkinTool setSKinColor:@"orange"];
[self changeImages];
}
- (IBAction)changeToBlueSkin {
[SkinTool setSKinColor:@"blue"];
[self changeImages];
}
- (IBAction)changeToRedSkin {
[SkinTool setSKinColor:@"red"];
[self changeImages];
}
- (IBAction)changeToGreenSkin {
[SkinTool setSKinColor:@"green"];
[self changeImages];
}
- (void)changeImages
{
self.faceImageView.image = [SkinTool skinToolWithImageName:@"face"];
self.heartImageView.image = [SkinTool skinToolWithImageName:@"heart"];
self.rectImageView.image = [SkinTool skinToolWithImageName:@"rect"];
}
@end