主题切换在前几年很火的一个技术,俗称换肤!
今天我们来实现下这个主题是怎么切换的。
主体思路:
1.要求的图片名是一样的,换的是文件夹的名称,就是换的路径,所以要求的主题路径是不一样的。
2.创建一个工具类,根据plist文件找到对应的主题,实现图片路径的加载。并且创建一个对外的属性,就是切换的主题名。
3.监听切换的主题,对此发送通知,进行实时修改。
另外要导入的图片要真实的文件夹,图片名称一样,放在不同的文件夹下面。
上代码:
工具类:.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{
//这里可以做代码优化,自创一个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{
//移除自创标题
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.监听文件夹的名称
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
先看看效果:
···