降低項目耦合性之換膚功能的解析封裝

<h1>我們要先知道</h1>
<h4>1.皮膚是什么裆悄? 圖片
2.換膚是什么 ? 改變圖片
3.通過什么方式改變萎庭? 管理類加上通知的方法
4.改變那些圖片霜医?tabbar,navigation驳规,我的肴敛,以及頁面的背景。
5.換膚應(yīng)用在那里达舒? (1)節(jié)假日切換主題(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:@"默認(rèn)"]) {
    themeName = nil;
    }
    // 記錄當(dāng)前主題名字
    themeManager.themeName = themeName;
    [[NSNotificationCenter defaultCenter] postNotificationName:kThemeChangedNotification object:nil];

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

// 重新加載數(shù)據(jù)顯示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

資源目錄結(jié)構(gòu)


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

源碼地址

其他背景統(tǒng)一封裝在baseviewcontroller中用。無法封裝的背景只能另接收通知處理趾代。當(dāng)然是肯定可以封裝的贯底。hiden方法控制在唯一界面顯示。有更好的方法歡迎指點撒强。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末禽捆,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子飘哨,更是在濱河造成了極大的恐慌胚想,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,406評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件芽隆,死亡現(xiàn)場離奇詭異浊服,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)胚吁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,395評論 3 398
  • 文/潘曉璐 我一進(jìn)店門牙躺,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人腕扶,你說我怎么就攤上這事孽拷。” “怎么了半抱?”我有些...
    開封第一講書人閱讀 167,815評論 0 360
  • 文/不壞的土叔 我叫張陵脓恕,是天一觀的道長膜宋。 經(jīng)常有香客問我,道長炼幔,這世上最難降的妖魔是什么秋茫? 我笑而不...
    開封第一講書人閱讀 59,537評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮江掩,結(jié)果婚禮上学辱,老公的妹妹穿的比我還像新娘。我一直安慰自己环形,他們只是感情好策泣,可當(dāng)我...
    茶點故事閱讀 68,536評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著抬吟,像睡著了一般萨咕。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上火本,一...
    開封第一講書人閱讀 52,184評論 1 308
  • 那天危队,我揣著相機(jī)與錄音,去河邊找鬼钙畔。 笑死茫陆,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的擎析。 我是一名探鬼主播簿盅,決...
    沈念sama閱讀 40,776評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼揍魂!你這毒婦竟也來了桨醋?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,668評論 0 276
  • 序言:老撾萬榮一對情侶失蹤现斋,失蹤者是張志新(化名)和其女友劉穎喜最,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體庄蹋,經(jīng)...
    沈念sama閱讀 46,212評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡瞬内,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,299評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了蔓肯。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片遂鹊。...
    茶點故事閱讀 40,438評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖蔗包,靈堂內(nèi)的尸體忽然破棺而出秉扑,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 36,128評論 5 349
  • 正文 年R本政府宣布舟陆,位于F島的核電站误澳,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏秦躯。R本人自食惡果不足惜忆谓,卻給世界環(huán)境...
    茶點故事閱讀 41,807評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望踱承。 院中可真熱鬧倡缠,春花似錦、人聲如沸茎活。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,279評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽载荔。三九已至盾饮,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間懒熙,已是汗流浹背丘损。 一陣腳步聲響...
    開封第一講書人閱讀 33,395評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留工扎,地道東北人徘钥。 一個月前我還...
    沈念sama閱讀 48,827評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像肢娘,于是被迫代替她去往敵國和親吏饿。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,446評論 2 359

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

  • *7月8日上午 N:Block :跟一個函數(shù)塊差不多蔬浙,會對里面所有的內(nèi)容的引用計數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,492評論 1 14
  • 技術(shù)無極限贞远,從菜鳥開始畴博,從源碼開始。 由于公司目前項目還是用OC寫的項目蓝仲,沒有升級swift 所以暫時SDWebI...
    充滿活力的早晨閱讀 12,656評論 0 2
  • 1俱病、禁止手機(jī)睡眠 [UIApplication sharedApplication].idleTimerDisab...
    FF_911閱讀 1,405評論 0 3
  • assert($a)檢查$a是否為false,是返回true getenv($str)獲取一個環(huán)境變量的值 php...
    iscona閱讀 240評論 0 1
  • 隨著科學(xué)技術(shù)的發(fā)展袱结,人們生活方式的改變亮隙,現(xiàn)代教育的形式也變得多元化。老師以前都是親手批改學(xué)生的作業(yè)垢夹,現(xiàn)在有一種新型...
    王櫟涵閱讀 794評論 2 1