iOS-簡易一鍵換膚

  • 換膚原理:
    • 1.切換背景圖片或者控件的背景色
    • 2.更改文字顏色
  • 需用到偏好設(shè)置存儲(chǔ)上一次的設(shè)置
    • 需注意是如果程序意外退出,數(shù)據(jù)不會(huì)被系統(tǒng)寫入到該文件,不過可以使用[[NSUserDefaults standardUserDefaults] synchronize]命令直接同步到文件里棘脐,來避免數(shù)據(jù)的丟失。
  • 更改控件的背景色用到plist存儲(chǔ)RGB顏色值
  • 使用換膚要寫在控制器的viewWillAppear方法中贯底,避免換膚沒有起作用比如tabBarController
  • 用到UIAppearance需要注意:
    • UIAppearance有個(gè)限制休建,就是如果想讓它生效袍镀,必須在下次裝載入app的主窗口時(shí)才能生效岗屏,所以辆琅,如果要通過UIAppearance動(dòng)態(tài)修改組件的屬性,那么可以把整個(gè)控制器的View先移到一邊,然后再添加到主窗口;

1.封裝的工具類,對(duì)外提供接口

#import <UIKit/UIKit.h>
#define skinColorKey @"skinColor"
@interface SkinTool : NSObject
+ (void)setSkincolor:(NSString *)skinColor;
+ (UIImage *)skinToolWithImageName:(NSString *)imageName;
+ (UIColor *)skinToolWithLabelColor;
@end

#import "SkinTool.h"
@implementation SkinTool

static NSString *_skinColor;
// 這個(gè)類第一次使用的時(shí)候調(diào)用,而且只會(huì)調(diào)用一次
+ (void)initialize
{
    NSString *value = [[NSUserDefaults standardUserDefaults] objectForKey:skinColorKey];
    if (value == nil) {
        value = @"red";
    }
    _skinColor = value;
}

+ (void)setSkincolor:(NSString *)skinColor
{
    _skinColor = skinColor;
    
    // 保存用戶選中的皮膚顏色
    [[NSUserDefaults standardUserDefaults] setObject:skinColor forKey:skinColorKey];
}
[[NSUserDefaults standardUserDefaults] synchronize];
// 返回一張圖片
+ (UIImage *)skinToolWithImageName:(NSString *)imageName
{
    // 拼接圖片路徑
    NSString *imagePath = [NSString stringWithFormat:@"skin/%@/%@",_skinColor,imageName];
    return [UIImage imageNamed:imagePath];
}

// 返回一個(gè)plist文件中RGB顏色
+ (UIColor *)skinToolWithLabelColor
{
    // 1.獲取對(duì)應(yīng)plist文件名稱
    NSString *plistName = [NSString stringWithFormat:@"skin/%@/bgColor.plist",_skinColor];
    
    // 2.獲取對(duì)應(yīng)plist文件路徑
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:plistName ofType:nil];
    
    // 3.讀取plist文件
    NSDictionary *colorDict = [NSDictionary dictionaryWithContentsOfFile:plistPath];
    NSString *colorStr = colorDict[@"labelBgColor"];
    
    // 4.獲取顏色數(shù)組
    NSArray *colorArr = [colorStr componentsSeparatedByString:@","];
    
    // 5.讀取對(duì)應(yīng)RGB
    NSInteger red = [colorArr[0] integerValue];
    NSInteger green = [colorArr[1] integerValue];
    NSInteger blue = [colorArr[2] integerValue];
    return [UIColor colorWithRed:red / 255.0 green:green / 255.0 blue:blue / 255.0 alpha:1.0];
}

2.在第一個(gè)控制器中

- (void)viewDidLoad {
    [super viewDidLoad];
  
    [self changeImage];
    [self setLabelTextColorAndButtonTitleColor];
}

- (void)setLabelTextColorAndButtonTitleColor{
    UIColor *color = [SkinTool skinToolWithLabelColor];
    [[UIButton appearance] setTitleColor:color forState:UIControlStateNormal];
    [[UILabel appearance] setTextColor:color];
}

- (IBAction)changeRedColor:(id)sender {
    [SkinTool setSkincolor:@"red"];

    [self setLabelTextColorAndButtonTitleColor];
   
    [self.tabBarController.view removeFromSuperview];
    [[UIApplication sharedApplication].keyWindow addSubview:self.tabBarController.view];
    
    [self changeImage];
}

- (IBAction)changeGreenColor:(id)sender {
    [SkinTool setSkincolor:@"green"];
    [self setLabelTextColorAndButtonTitleColor];
    
    [self.tabBarController.view removeFromSuperview];
    [[UIApplication sharedApplication].keyWindow addSubview:self.tabBarController.view];

    [self changeImage];
}
- (IBAction)changeBlueColor:(id)sender {
    
    [SkinTool setSkincolor:@"blue"];
    [self setLabelTextColorAndButtonTitleColor];
    
    [self.tabBarController.view removeFromSuperview];
    [[UIApplication sharedApplication].keyWindow addSubview:self.tabBarController.view];

    [self changeImage];
}
- (IBAction)chageOrangecolor:(id)sender {
    [SkinTool setSkincolor:@"orange"];
    [self changeImage];

}
- (void)changeImage
{
    self.faceImageView.image = [SkinTool skinToolWithImageName:@"face"];
    self.heartImageView.image = [SkinTool skinToolWithImageName:@"heart"];
    self.rectImageView.image = [SkinTool skinToolWithImageName:@"rect"];
}

3.在第二個(gè)控制器中

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self changeImages];
    self.testLabel.backgroundColor = [SkinTool skinToolWithLabelColor];
}
- (void)changeImages
{
    self.faceImageView.image = [SkinTool skinToolWithImageName:@"face"];
    self.heartImageView.image = [SkinTool skinToolWithImageName:@"heart"];
    self.rectImageView.image = [SkinTool skinToolWithImageName:@"rect"];
}

plist中RGB配置示例圖


test.png

測試效果如圖

demo.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市这刷,隨后出現(xiàn)的幾起案子婉烟,更是在濱河造成了極大的恐慌,老刑警劉巖暇屋,帶你破解...
    沈念sama閱讀 212,542評(píng)論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件隅很,死亡現(xiàn)場離奇詭異,居然都是意外死亡率碾,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,596評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門屋彪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來所宰,“玉大人,你說我怎么就攤上這事畜挥∽兄啵” “怎么了?”我有些...
    開封第一講書人閱讀 158,021評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵蟹但,是天一觀的道長躯泰。 經(jīng)常有香客問我,道長华糖,這世上最難降的妖魔是什么麦向? 我笑而不...
    開封第一講書人閱讀 56,682評(píng)論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮客叉,結(jié)果婚禮上诵竭,老公的妹妹穿的比我還像新娘话告。我一直安慰自己,他們只是感情好卵慰,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,792評(píng)論 6 386
  • 文/花漫 我一把揭開白布沙郭。 她就那樣靜靜地躺著,像睡著了一般裳朋。 火紅的嫁衣襯著肌膚如雪病线。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,985評(píng)論 1 291
  • 那天鲤嫡,我揣著相機(jī)與錄音送挑,去河邊找鬼。 笑死泛范,一個(gè)胖子當(dāng)著我的面吹牛让虐,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播罢荡,決...
    沈念sama閱讀 39,107評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼赡突,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了区赵?” 一聲冷哼從身側(cè)響起惭缰,我...
    開封第一講書人閱讀 37,845評(píng)論 0 268
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎笼才,沒想到半個(gè)月后漱受,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,299評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡骡送,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,612評(píng)論 2 327
  • 正文 我和宋清朗相戀三年昂羡,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片摔踱。...
    茶點(diǎn)故事閱讀 38,747評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡虐先,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出派敷,到底是詐尸還是另有隱情蛹批,我是刑警寧澤,帶...
    沈念sama閱讀 34,441評(píng)論 4 333
  • 正文 年R本政府宣布篮愉,位于F島的核電站腐芍,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏试躏。R本人自食惡果不足惜猪勇,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,072評(píng)論 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望颠蕴。 院中可真熱鬧埠对,春花似錦络断、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,828評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至襟沮,卻和暖如春锥惋,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背开伏。 一陣腳步聲響...
    開封第一講書人閱讀 32,069評(píng)論 1 267
  • 我被黑心中介騙來泰國打工膀跌, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人固灵。 一個(gè)月前我還...
    沈念sama閱讀 46,545評(píng)論 2 362
  • 正文 我出身青樓捅伤,卻偏偏與公主長得像,于是被迫代替她去往敵國和親巫玻。 傳聞我的和親對(duì)象是個(gè)殘疾皇子丛忆,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,658評(píng)論 2 350

推薦閱讀更多精彩內(nèi)容