目前有很多APP都有着节假日换肤的功能,不同的人有这不同的实现思路,下面简单的概述一下本人的实现方式。
1、确定好需要变动的内容,将需要随时改变的地方尽量写成公共的(注:在需要改变时只需修改某一处即可,方便快捷),当然也可以使用判断条件,在需要变动时执行更改的那部分代码即可。如:导航栏的颜色、taBar 按钮的图片以及颜色、首页某个模块的样式等。
2、确定好需要变动的内容后,根据变动的需求将变动的内容以字典的形式写成plist文件。如:我需要修改taBar 按钮的图片以及颜色 就可以把plist文件写成一下形式:
3、新建一个文件夹 如命名为:tabbarItems ,将新的图片以及刚刚写好的plist文件放在该文件夹里面,如:
4、然后将该文件夹进行压缩,将压缩文件交给后台,让后台的同事找个地方存放起来,然后让后台在适当的接口返回一个参数用于是否需要进行下载更新操作,如首页返回 isDownloadSkin ,并且 新增一个接口用于下载你给后台的那个压缩文件,如果 isDownloadSkin == YES ,就 开辟子线程进行后台下载操作。一下为下载解压部分代码:
- (void)downLoadAndUnzip{
//1.创建会话管理者
AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];
NSURL *url = [NSURL URLWithString:@"http://1234567/data/upload/android/skin.zip"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//2.下载文件
/*
第一个参数:请求对象
第二个参数:progress 进度回调 downloadProgress
第三个参数:destination 回调(目标位置)
有返回值
targetPath:临时文件路径
response:响应头信息
第四个参数:completionHandler 下载完成之后的回调
filePath:最终的文件路径
*/
NSURLSessionDownloadTask *download = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
//监听下载进度
//completedUnitCount 已经下载的数据大小
//totalUnitCount 文件数据的中大小
NSLog(@"%f",1.0 *downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [cachesPath stringByAppendingPathComponent:response.suggestedFilename];
return [NSURL fileURLWithPath:path];
// return [NSURL fileURLWithPath:DocumentPath];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
_skinStatus =@"dowlond";
NSString *imgFilePath = [filePath path];// 将NSURL转成NSString
NSArray *documentArray = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *path = [[documentArray lastObject] stringByAppendingPathComponent:@"Preferences"];
[self releaseZipFilesWithUnzipFileAtPath:imgFilePath Destination:path];
//解压完成, 至此皮肤资源已经完整缓存到沙盒, 删除zip包, 保存skinStatus
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:imgFilePath error:nil];
NSLog(@"%@ error:%@",response,error);
NSLog(@"-----------------%@",filePath);
}];
//3.执行Task
[download resume];
}
// 解压
- (void)releaseZipFilesWithUnzipFileAtPath:(NSString *)zipPath Destination:(NSString *)unzipPath{
NSError *error;
if ([SSZipArchive unzipFileAtPath:zipPath toDestination:unzipPath overwrite:YES password:nil error:&error delegate:nil]) {
NSLog(@"解压 ---------success");
NSLog(@"unzipPath = %@",unzipPath);
}else {
NSLog(@"解压 ---------%@",error);
}
}
5、下载解压缩成功后 就可以对需要更换的地方进行替换。如更换tabBrItemSkin:例:
for (NSInteger i = 0; i <[WKSkinTool shareSkinTool].tabBrItemSkinArr.count; i ++) {// 换肤
WKSkinItemModel * model =[WKSkinTool shareSkinTool].tabBrItemSkinArr[i];
if (i ==0) {
[self itemWithchildViewController:[[WKMallViewController alloc] init ]withTitle:model.title normelImage:model.titleIconNor selectedImage:model.titleIconSel normelColor:WKColor(model.norRed, model.norGreen, model.norBlue) selectedColor:WKColor(model.selectRed, model.selectGreen, model.selectBlue)] ;
}else if (i == 1){
[self itemWithchildViewController:[[WKDiscoverViewController alloc]init]
withTitle:model.title normelImage:model.titleIconNor selectedImage:model.titleIconSel normelColor:WKColor(model.norRed, model.norGreen, model.norBlue) selectedColor:WKColor(model.selectRed, model.selectGreen, model.selectBlue)] ;
}else if (i == 2){
[self itemWithchildViewController:[[WKChatViewController alloc] init ] withTitle:model.title normelImage:model.titleIconNor selectedImage:model.titleIconSel normelColor:WKColor(model.norRed, model.norGreen, model.norBlue) selectedColor:WKColor(model.selectRed, model.selectGreen, model.selectBlue)] ;
}else{
[self itemWithchildViewController:[[WKMeViewController alloc] init ]withTitle:model.title normelImage:model.titleIconNor selectedImage:model.titleIconSel normelColor:WKColor(model.norRed, model.norGreen, model.norBlue) selectedColor:WKColor(model.selectRed, model.selectGreen, model.selectBlue)] ;
}
}
}
-(UIViewController *)itemWithchildViewController:(UIViewController *)childViewController withTitle:(NSString *)title normelImage:(NSString*)normelImage selectedImage:(NSString *)selectedImage normelColor:(UIColor*)normelColor selectedColor:(UIColor*)selectedColor {
NSString * normelIm = [NSString stringWithFormat:@"%@/tabbarItems/%@",[[WKSkinTool shareSkinTool] getSkinPath ],normelImage];
childViewController.tabBarItem.image = [[UIImage imageWithContentsOfFile:normelIm]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
NSString * selectedIm = [NSString stringWithFormat:@"%@/tabbarItems/%@",[[WKSkinTool shareSkinTool] getSkinPath ],selectedImage];
childViewController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedIm] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// 设置 Itme的 title 及文字颜色
childViewController.tabBarItem.title = title;
NSMutableDictionary *nalAttr = [NSMutableDictionary dictionary];
nalAttr[NSForegroundColorAttributeName] = normelColor;
NSMutableDictionary * attr = [NSMutableDictionary dictionary];
attr[NSForegroundColorAttributeName] =selectedColor;
[ childViewController.tabBarItem setTitleTextAttributes:nalAttr forState:UIControlStateNormal];
[childViewController.tabBarItem setTitleTextAttributes:attr forState:UIControlStateSelected];
// 用导航控制器 包装 只控制器
WKBaseNavigationController * nav = [[WKBaseNavigationController alloc]initWithRootViewController:childViewController];
self.baseNavigationController=nav;
// 将只控制器添加到 TabBarController
[self addChildViewController: nav];
return childViewController;
}
shareSkinTool 工具类:
/******* 获取皮肤所在最外层公共路径 *******/
-(NSString *)getSkinPath{
self.unzipPath=[[self getPath] stringByAppendingPathComponent:@"Preferences/skin"];
return self.unzipPath ;
}
-(NSMutableArray *)tabBrItemSkinArr{ //tabbar 皮肤主题数组
if (!_tabBrItemSkinArr) {
NSString *filepath=[[self getPath] stringByAppendingPathComponent:@"Preferences/skin/tabbarItems/mallSkin.plist"];
NSArray * temp = [NSArray arrayWithContentsOfFile:filepath];
_tabBrItemSkinArr = [WKSkinItemModel mj_objectArrayWithKeyValuesArray:temp];
}
return _tabBrItemSkinArr;
}
-(NSString * )getPath{
NSArray *patharray = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *path = [patharray objectAtIndex:0];
return path;
}