#import <UIKit/UIKit.h>
//專門寫常量值的
// 以下的格式, 只是對常量的聲明
//extern: 代表其他類可以引用
extern NSString *const LEESkinToolLabelTextDayColor;
extern NSString *const LEESkinToolLabelBackgroundDayColor;
@interface LEESkinTools : NSObject
/** 返回對應的皮膚的圖像*/
+ (UIImage *)imageWithImageName:(NSString *)imageName;
/** 保存皮膚信息*/
+ (void)saveSkinName:(NSString *)skinName;
/** 返回制定標識符所對應的顏色*/
+ (UIColor *)colorWithName:(NSString *)name;
@end
#import "LEESkinTools.h"
//專門寫常量值的
// 以下的格式, 只是對常量的聲明
NSString *const LEESkinToolLabelTextDayColor = @"labelTextDayColor";
NSString *const LEESkinToolLabelBackgroundDayColor = @"labelBackgroundDayColor";
static NSString *_skinName;
//#1. 創建一個static的可變字典
static NSMutableDictionary *_colorDict;
@implementation LEESkinTools
/**
保證代碼只加載一次:
1. 多線程 --> dispatchOnce
2. 只調用一次的方法
*/
/** load方法, 只要頭文件參與了編譯就會調用此方法 */
+ (void)load
{
NSLog(@"%s",__func__);
}
/** 類的實例創建時, 纔會調用此方法*/
+ (void)initialize
{
//1. 讀取偏好設置信息 --> 訪問磁盤是耗性能的, 所以只需要加載一次即可
// _skinName = [[NSUserDefaults standardUserDefaults] objectForKey:@"skinName"];
if (_skinName == nil) {
_skinName = @"green";
}
//#2. 可變字典做初始化
_colorDict = [NSMutableDictionary dictionary];
//#3. 加載顏色plist
[self loadColorDict];
}
/** 返回對應的皮膚的圖像*/
+ (UIImage *)imageWithImageName:(NSString *)imageName
{
//1. 拼接文件路徑
NSString *imageStr = [NSString stringWithFormat:@"skin/%@/%@",_skinName,imageName];
//2. 返回一個指定的圖像
return [UIImage imageNamed:imageStr];
}
/** 保存皮膚信息*/
+ (void)saveSkinName:(NSString *)skinName
{
//當皮膚讀取改變成一次之後, 那麼我們做皮膚切換保存操作, 原來的_skinName
//更改皮膚
_skinName = skinName;
// 當設置了不同皮膚時, 我們還需要將皮膚plist顏色信息進行重新加載
[self loadColorDict];
//保存皮膚
[[NSUserDefaults standardUserDefaults] setObject:skinName forKey:@"skinName"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
/** 返回制定標識符所對應的顏色*/
+ (UIColor *)colorWithName:(NSString *)name
{
NSLog(@"%@",name);
return _colorDict[name];
}
/** 初始化時需要調用一次, 來加載字典及轉換顏色*/
+ (void)loadColorDict
{
//1. 獲取plist列表
//2. 顏色轉換(255,0,0,1 --> UIColor對象)
//1. plist只需要加載一次
//2. 將轉換後的UIColor做保存
//1. 獲取plist路徑
NSString *pathStr = [NSString stringWithFormat:@"skin/%@/SkinColors.plist",_skinName];
NSString *path = [[NSBundle mainBundle] pathForResource:pathStr ofType:nil];
//2. 獲取字典
NSDictionary *colorDict = [NSDictionary dictionaryWithContentsOfFile:path];
//3. 直接獲取所有顏色值將其轉換成UIColor對象
[colorDict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull colorStr, BOOL * _Nonnull stop) {
//4. 截取字符串
NSArray *colorArr = [colorStr componentsSeparatedByString:@","];
//5. 創建顏色賦值給緩存字典
CGFloat r = [colorArr[0] doubleValue];
CGFloat g = [colorArr[1] doubleValue];
CGFloat b = [colorArr[2] doubleValue];
CGFloat a = [colorArr[3] doubleValue];
//字典的裏面存的是轉換好的UIColor對象, 當設置切換皮膚後, 需要重新調用此方法, 進行_colorDict的更新
_colorDict[key] = [UIColor colorWithRed:r / 255.0 green:g / 255.0 blue:b / 255.0 alpha:a];
}];
}
@end
storyboard 中的空間拖線在這裏就省略了,直接調用工具類實現
#import "ViewController.h"
#import "LEESkinTools.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *faceView;
@property (weak, nonatomic) IBOutlet UIImageView *heartView;
@property (weak, nonatomic) IBOutlet UIImageView *rectView;
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 啓動時加載皮膚
[self changeSkinWithSkinName:nil];
}
- (IBAction)redClick:(id)sender {
// 切換皮膚
[self changeSkinWithSkinName:@"red"];
}
- (IBAction)greenClick:(id)sender {
// 切換皮膚
[self changeSkinWithSkinName:@"green"];
}
- (IBAction)blueClick:(id)sender {
// 切換皮膚
[self changeSkinWithSkinName:@"blue"];
}
- (IBAction)orangeClick:(id)sender {
// 切換皮膚
[self changeSkinWithSkinName:@"orange"];
}
//抽取公用方法
- (void)changeSkinWithSkinName:(NSString *)skinName
{
// 判斷如果皮膚名稱傳了空, 那麼不用管保存
if (skinName != nil) {
[LEESkinTools saveSkinName:skinName];
}
// 切換圖像
self.faceView.image = [LEESkinTools imageWithImageName:@"face"];
self.heartView.image = [LEESkinTools imageWithImageName:@"heart"];
self.rectView.image = [LEESkinTools imageWithImageName:@"rect"];
// 切換文字顏色
self.label.textColor = [LEESkinTools colorWithName:LEESkinToolLabelTextDayColor];
// 切換背景顏色
self.label.backgroundColor = [LEESkinTools colorWithName:LEESkinToolLabelBackgroundDayColor];
}
@end