主題切換在前幾年很火的一個技術寂祥,俗稱換膚贷掖!
今天我們來實現下這個主題是怎么切換的。
主體思路:
1.要求的圖片名是一樣的孽糖,換的是文件夾的名稱枯冈,就是換的路徑,所以要求的主題路徑是不一樣的办悟。
2.創(chuàng)建一個工具類,根據plist文件找到對應的主題尘奏,實現圖片路徑的加載。并且創(chuàng)建一個對外的屬性病蛉,就是切換的主題名炫加。
3.監(jiān)聽切換的主題,對此發(fā)送通知铺然,進行實時修改俗孝。
另外要導入的圖片要真實的文件夾,圖片名稱一樣魄健,放在不同的文件夾下面赋铝。
上代碼:
工具類:.h
//
// StyleTools.h
// ChangeStyle-Demo
//
// Created by mac on 16/8/25.
// Copyright ? 2016年 mac. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface StyleTools : NSObject
@property(nonatomic,copy)NSString *style;
+(instancetype)sharedManager;
-(UIImage *)getImageName:(NSString *)name;
@end
//
// StyleTools.m
// ChangeStyle-Demo
//
// Created by mac on 16/8/25.
// Copyright ? 2016年 mac. All rights reserved.
//
#import "StyleTools.h"
static StyleTools *instance = nil;
@implementation StyleTools
+(instancetype)sharedManager{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[StyleTools alloc] init];
});
return instance;
}
- (instancetype)init
{
//設置初始主題(這里可以保存在沙盒中,以防止下次取出的時候又回到初始主題沽瘦,本人就沒寫了)
self = [super init];
if (self) {
_style = @"村落";
}
return self;
}
-(NSString *)loadImagePath{
//獲取主題數據路徑
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Theme" ofType:@"plist"];
//字典
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
//主題名
NSString *styleName = [dic objectForKey:_style];
//真實路徑
NSString *stylePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:styleName];
return stylePath;
}
-(UIImage *)getImageName:(NSString *)imageName{
//
NSString *stylePath = [self loadImagePath];
//圖片的路徑
NSString *imagePath = [NSString stringWithFormat:@"%@/%@",stylePath,imageName];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
//返回圖片
return image;
}
-(void)setStyle:(NSString *)style{
_style = style;
[[NSNotificationCenter defaultCenter] postNotificationName:@"StyleName" object:nil];
}
@end
標簽欄的實現柬甥。
//
// BaseViewController.m
// ChangeStyle-Demo
//
// Created by mac on 16/8/25.
// Copyright ? 2016年 mac. All rights reserved.
//
#import "BaseViewController.h"
#import "StyleTools.h"
@interface BaseViewController (){
NSArray *array;
}
@end
@implementation BaseViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self loadData];
[self createBase];
}
-(void)loadData{
array = [NSArray array];
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Theme" ofType:@"plist"];
//字典
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
//獲取到所有鍵
array = [dic allKeys];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self removeSystemTabbar];
}
-(void)createBase{
StyleTools *manager = [StyleTools sharedManager];
NSArray *imgArr = @[
@"home_tab_icon_1.png",
@"home_tab_icon_2.png",
@"home_tab_icon_3.png",
@"home_tab_icon_4.png",
@"home_tab_icon_5.png"
];
for (int i = 0; i<imgArr.count; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[manager getImageName:imgArr[i]] forState:UIControlStateNormal];
button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/imgArr.count * i, 3, 50, 50);
button.tag = 1000 + i;
[button addTarget:self action:@selector(changeAction:) forControlEvents:UIControlEventTouchUpInside];
[self.tabBar addSubview:button];
}
//接收通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadImage) name:@"StyleName" object:nil];
[self loadImage];
}
-(void)loadImage{
//這里可以做代碼優(yōu)化,自創(chuàng)一個button類其垄,進行封裝就不用寫五個了
StyleTools *manager = [StyleTools sharedManager];
NSArray *imgArr = @[
@"home_tab_icon_1.png",
@"home_tab_icon_2.png",
@"home_tab_icon_3.png",
@"home_tab_icon_4.png",
@"home_tab_icon_5.png"
];
UIButton *button1 = (UIButton *)[self.view viewWithTag:1000];
[button1 setImage:[manager getImageName:imgArr[0] ]forState:UIControlStateNormal];
UIButton *button2 = (UIButton *)[self.view viewWithTag:1001];
[button2 setImage:[manager getImageName:imgArr[1] ]forState:UIControlStateNormal];
UIButton *button3 = (UIButton *)[self.view viewWithTag:1002];
[button3 setImage:[manager getImageName:imgArr[2] ]forState:UIControlStateNormal];
UIButton *button4 = (UIButton *)[self.view viewWithTag:1003];
[button4 setImage:[manager getImageName:imgArr[3] ]forState:UIControlStateNormal];
UIButton *button5 = (UIButton *)[self.view viewWithTag:1004];
[button5 setImage:[manager getImageName:imgArr[4] ]forState:UIControlStateNormal];
self.tabBar.backgroundImage = [manager getImageName:@"mask_navbar.png"];
}
-(void)changeAction:(UIButton *)button{
// NSLog(@"%@",button.titleLabel.text);
NSInteger index = button.tag - 1000;
StyleTools *manager = [StyleTools sharedManager];
//將主題獲取到?疗选!绿满!
manager.style = array[index];
}
-(void)removeSystemTabbar{
//移除自創(chuàng)標題
for (UIView *view in self.tabBar.subviews) {
if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
[view removeFromSuperview];
}
}
}
#pragma mark -手動釋放通知
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"StyleName" object:nil];
}
@end
navigationcontroller中
//
// NavigationViewController.m
// ChangeStyle-Demo
//
// Created by mac on 16/8/25.
// Copyright ? 2016年 mac. All rights reserved.
//
#import "NavigationViewController.h"
#import "StyleTools.h"
@interface NavigationViewController ()
@end
@implementation NavigationViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadImage) name:@"StyleName" object:nil];
[self loadImage];
}
-(void)loadImage{
StyleTools *manager = [StyleTools sharedManager ];
self.view.backgroundColor = [UIColor colorWithPatternImage:[manager getImageName:@"mask_titlebar64@2x.jpg"]];
}
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"StyleName" object:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
viewcontroller中
//
// ViewController.m
// ChangeStyle-Demo
//
// Created by mac on 16/8/25.
// Copyright ? 2016年 mac. All rights reserved.
//
#import "ViewController.h"
#import "StyleTools.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/*
切換主題:
主體思路就是切換文件夾
1.獲取到真實文件夾
2.監(jiān)聽文件夾的名稱
3.切換
*/
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadImage) name:@"StyleName" object:nil];
[self loadImage];
}
-(void)loadImage{
StyleTools *manager = [StyleTools sharedManager ];
self.view.backgroundColor = [UIColor colorWithPatternImage:[manager getImageName:@"bg_home@2x.jpg"]];
}
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"StyleName" object:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
先看看效果:
stylechange1.gif
···