热门标签 | HotTags
当前位置:  开发笔记 > IOS > 正文

一步一步实现iOS主题皮肤切换效果

这篇文章主要为大家详细介绍了一步一步实现iOS主题皮肤切换效果的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了iOS主题皮肤切换代码,供大家参考,具体内容如下

1. 主题皮肤功能切换介绍
主题切换就是根据用户设置不同的主题,来动态改变用户的界面,通常会改变navigationBar背景图片、tabBar背景图片、tabBar中的按钮的图片和选中的背景图片、navigationItem.title 标题的字体颜色、UI中其他元素控件

下载源代码地址: http://xiazai.jb51.net/201609/yuanma/ThemeSkinSetup(jb51.net).rar

2.项目目录结构及实现效果截图




3. 具体实现步骤

1.将image文件夹(group)和 Skins拖入到项目工程中的资源文件夹中
2.创建BaseViewController
3.配置theme.plist
4.事项项目所需的基本框架供能,并实现主题的tableView功能
5.创建主题管理器:ThemeManager
6.自定义ThemeTabBarItem 控件
7.创建UI工厂: UIFactory
8. 实现tableView中的didSelected事件完成主题切换
9.记录用户选择的主题,以便用户下次启动时是上次设置的主题

1.创建BaseViewController

#import  
@interface BaseViewController : UIViewController 
 
- (void) reloadThemeImage; 
@end 

#import "BaseViewController.h" 
 
#import "ThemeManager.h" 
#import "NotificationMacro.h" 
 
@interface BaseViewController () 
 
@end 
 
@implementation BaseViewController 
- (id) init { 
 if (self == [super init]) { 
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeChangedNotfication:) name:kThemeChangedNotification object:nil]; 
 } 
  
 [self reloadThemeImage]; 
 return self; 
} 
 
- (void)viewDidLoad { 
 [super viewDidLoad]; 
 [self reloadThemeImage]; 
} 
 
- (void)didReceiveMemoryWarning { 
 [super didReceiveMemoryWarning]; 
 // Dispose of any resources that can be recreated. 
} 
 
- (void) themeChangedNotfication:(NSNotification *)notification { 
 [self reloadThemeImage]; 
} 
 
- (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]; 
} 
@end 

2. 实现AppDelegate

#import "AppDelegate.h" 
 
#import "MainViewController.h" 
#import "ThemeManager.h" 
#import "NotificationMacro.h" 
 
@interface AppDelegate () 
 
@end 
 
@implementation AppDelegate 
 
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
 [self initUserDefaultConfig]; 
  
 MainViewController * rootViewCOntroller= [[MainViewController alloc] init]; 
 self.window.rootViewCOntroller= rootViewController; 
  
 return YES; 
} 
 
 
- (void) initUserDefaultConfig { 
 NSString * themeName = [[NSUserDefaults standardUserDefaults] objectForKey:kThemeNameKey]; 
 ThemeManager * themeManager = [ThemeManager sharedThemeManager]; 
 themeManager.themeName = themeName; 
} 

#import "MainViewController.h" 
 
#import "HomeViewController.h" 
#import "MessageViewController.h" 
#import "MineViewController.h" 
 
#import "UIFactory.h" 
 
 
@interface MainViewController () 
 
@end 
 
@implementation MainViewController 
 
- (id) init { 
 if (self = [super init]) { 
  [self initTabBarUI]; 
 } 
  
 return self; 
} 
 
- (void)viewDidLoad { 
 [super viewDidLoad]; 
  
} 
 
- (void)didReceiveMemoryWarning { 
 [super didReceiveMemoryWarning]; 
} 
 
 
- (void) initTabBarUI { 
 // 主页 
 HomeViewController * homeViewCOntroller= [[HomeViewController alloc] init]; 
 UINavigationController * homeNavigatiOnController= [[UINavigationController alloc] initWithRootViewController:homeViewController]; 
// UITabBarItem * homeTabBarItem = [[UITabBarItem alloc] initWithTitle:@"主页" image:[UIImage imageNamed:@"tabbar_home"] selectedImage:[UIImage imageNamed:@"tabbar_home_selected"]]; 
 UITabBarItem * homeTabBarItem = [UIFactory createTabBarItemWithTitle:@"主页" imageName:@"tabbar_home" selectedImage:@"tabbar_home_selected"]; 
 homeNavigationController.tabBarItem = homeTabBarItem; 
  
 // 消息(中心) 
 MessageViewController * messageViewCOntroller= [[MessageViewController alloc] init]; 
 UINavigationController * messageNavigatiOnController= [[UINavigationController alloc] initWithRootViewController:messageViewController]; 
// UITabBarItem * messageTabBarItem = [[UITabBarItem alloc] initWithTitle:@"消息" image:[UIImage imageNamed:@"tabbar_message_center"] selectedImage:[UIImage imageNamed:@"tabbar_message_center_selected"]]; 
 UITabBarItem * messageTabBarItem = [UIFactory createTabBarItemWithTitle:@"消息" imageName:@"tabbar_message_center" selectedImage:@"tabbar_message_center_selected"]; 
 messageNavigationController.tabBarItem = messageTabBarItem; 
  
 // 我 
 MineViewController * mineViewCOntroller= [[MineViewController alloc] init]; 
 UINavigationController * mineNavigatiOnController= [[UINavigationController alloc] initWithRootViewController:mineViewController]; 
// UITabBarItem * mineTabBarItem = [[UITabBarItem alloc] initWithTitle:@"我" image:[UIImage imageNamed:@"tabbar_profile"] selectedImage:[UIImage imageNamed:@"tabbar_profile_selected"]]; 
 UITabBarItem * mineTabBarItem = [UIFactory createTabBarItemWithTitle:@"我" imageName:@"tabbar_profile" selectedImage:@"tabbar_profile_selected"]; 
  
  
 mineNavigationController.tabBarItem = mineTabBarItem; 
 NSArray * viewCOntrollers= @[homeNavigationController, messageNavigationController, mineNavigationController]; 
 self.viewCOntrollers= viewControllers; 
} 
 
 
@end 

3. 创建主题管理器

#import  
#import  
 
@interface ThemeManager : NSObject 
 
@property (nonatomic, copy) NSString * themeName;   // 主题名字 
@property (nonatomic, retain) NSDictionary * themePlistDict; // 主题属性列表字典 
 
+ (ThemeManager *) sharedThemeManager; 
 
- (UIImage *) themeImageWithName:(NSString *)imageName; 
@end 

#import  
#import  
 
@interface ThemeManager : NSObject 
 
@property (nonatomic, copy) NSString * themeName;   // 主题名字 
@property (nonatomic, retain) NSDictionary * themePlistDict; // 主题属性列表字典 
 
+ (ThemeManager *) sharedThemeManager; 
 
- (UIImage *) themeImageWithName:(NSString *)imageName; 
@end 
#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 

4. 创建主题按钮 ThemeTabBarItem

#import  
@interface ThemeTabBarItem : UITabBarItem 
 
@property (nonatomic, copy) NSString * imageName; 
@property (nonatomic, copy) NSString * selectedImageName; 
 
 
- (id) initWithTitle:(NSString *)title imageName:(NSString *)imageName selectedImage:(NSString *)selectedImageName; 
 
@end  

#import "ThemeTabBarItem.h" 
#import "ThemeManager.h" 
#import "NotificationMacro.h" 
 
@implementation ThemeTabBarItem 
 
// 初始化时注册观察者 
- (id) init { 
 if (self = [super init]) { 
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeChangedNotification:) name:kThemeChangedNotification object:nil]; 
 } 
  
 return self; 
} 
 
- (id) initWithTitle:(NSString *)title imageName:(NSString *)imageName selectedImage:(NSString *)selectedImageName { 
 if (self = [self init]) { 
  self.title = title; 
  self.imageName = imageName;   // 此时会调用[self setImageName:imageName] ---> [self reloadThemeImage] --->[self setImage:image] 
  self.selectedImageName = selectedImageName;// 此时会调用[self setSelectedImageName:selectedImageName]; 
 } 
  
 return self; 
} 
 
 
#pragma mark - 
#pragma mark - Override Setter 
- (void) setImageName:(NSString *)imageName { 
 if (_imageName != imageName) { 
  _imageName = imageName; 
 } 
  
 [self reloadThemeImage]; 
} 
 
- (void) setSelectedImageName:(NSString *)selectedImageName { 
 if (_selectedImageName != selectedImageName) { 
  _selectedImageName = selectedImageName; 
 } 
  
 [self reloadThemeImage]; 
} 
 
 
 
// 主题改变之后重新加载图片 
- (void)themeChangedNotification:(NSNotification *)notification { 
 [self reloadThemeImage]; 
} 
 
- (void)reloadThemeImage { 
 ThemeManager * themeManager = [ThemeManager sharedThemeManager]; 
  
 if (self.imageName != nil) { 
  UIImage * image = [themeManager themeImageWithName:self.imageName]; 
  [self setImage:image]; 
 } 
  
 if (self.selectedImageName != nil) { 
  UIImage * selectedImage = [themeManager themeImageWithName:self.selectedImageName]; 
  [self setSelectedImage:selectedImage]; 
 } 
} 
 
- (void) dealloc { 
 [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 

5. 创建UI工厂

#import  
#import  
 
@interface UIFactory : NSObject 
 
+ (UITabBarItem *) createTabBarItemWithTitle:(NSString *)title imageName:(NSString *)imageName selectedImage:(NSString *)selectedImageName; 
 
 
@end 
#import  
#import  
 
@interface UIFactory : NSObject 
 
+ (UITabBarItem *) createTabBarItemWithTitle:(NSString *)title imageName:(NSString *)imageName selectedImage:(NSString *)selectedImageName; 
 
 
@end 
#import "UIFactory.h" 
 
#import "ThemeTabBarItem.h" 
@implementation UIFactory 
 
+ (UITabBarItem *) createTabBarItemWithTitle:(NSString *)title imageName:(NSString *)imageName selectedImage:(NSString *)selectedImageName { 
 ThemeTabBarItem * themeTabBarItem = [[ThemeTabBarItem alloc] initWithTitle:title imageName:imageName selectedImage:selectedImageName]; 
  
 return themeTabBarItem; 
} 
@end 

6. 实现选中单元格的事件

#import "BaseViewController.h" 
 
@interface MineViewController : BaseViewController  
 
 
@property (weak, nonatomic) IBOutlet UITableView *tableView; 
 
@property (nonatomic, retain) NSMutableArray * themeDataSource; 
@end 

#import "BaseViewController.h" 
 
@interface MineViewController : BaseViewController  
 
 
@property (weak, nonatomic) IBOutlet UITableView *tableView; 
 
@property (nonatomic, retain) NSMutableArray * themeDataSource; 
@end 
#import "MineViewController.h" 
 
#import "ThemeManager.h" 
#import "NotificationMacro.h" 
 
@interface MineViewController () 
 
@end 
 
@implementation MineViewController 
 
- (void)viewDidLoad { 
 [super viewDidLoad]; 
 self.title = @"我"; 
  
 ThemeManager * themeManager = [ThemeManager sharedThemeManager]; 
 _themeDataSource = [NSMutableArray arrayWithArray:themeManager.themePlistDict.allKeys]; 
} 
 
- (void)didReceiveMemoryWarning { 
 [super didReceiveMemoryWarning]; 
 // Dispose of any resources that can be recreated. 
} 
 
 
 
#pragma mark - 
#pragma mark - UITableViewDelegate 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
 
 return self.themeDataSource.count; 
} 
 
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
 static NSString * Identifier = @"Cell"; 
 UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:Identifier]; 
 if (cell == nil) { 
  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier]; 
 } 
  
 NSString * text = self.themeDataSource[indexPath.row]; 
 cell.textLabel.text = text; 
  
 ThemeManager * themeManager = [ThemeManager sharedThemeManager]; 
 NSString * currentTheme = themeManager.themeName; 
 if (currentTheme == nil) { 
  currentTheme = @"默认"; 
 } 
 if ([currentTheme isEqualToString:text]) { 
  cell.accessoryType = UITableViewCellAccessoryCheckmark; 
 } else { 
  cell.accessoryType = UITableViewCellAccessoryNone; 
 } 
  
 return cell; 
} 
 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
  
 ThemeManager * themeManager = [ThemeManager sharedThemeManager]; 
 NSString * themeName = self.themeDataSource[indexPath.row]; 
  
 if ([themeName isEqualToString:@"默认"]) { 
  themeName = nil; 
 } 
  
 // 记录当前主题名字 
 themeManager.themeName = themeName; 
 [[NSNotificationCenter defaultCenter] postNotificationName:kThemeChangedNotification object:nil]; 
  
  
 // 主题持久化 
 NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults]; 
 [userDefaults setObject:themeName forKey:kThemeNameKey]; 
 [userDefaults synchronize]; 
  
 // 重新加载数据显示UITableViewCellAccessoryCheckmark 显示选中的对号 v 
 [self.tableView reloadData]; 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


推荐阅读
  • 使用nodejs爬取b站番剧数据,计算最佳追番推荐
    本文介绍了如何使用nodejs爬取b站番剧数据,并通过计算得出最佳追番推荐。通过调用相关接口获取番剧数据和评分数据,以及使用相应的算法进行计算。该方法可以帮助用户找到适合自己的番剧进行观看。 ... [详细]
  • Monkey《大话移动——Android与iOS应用测试指南》的预购信息发布啦!
    Monkey《大话移动——Android与iOS应用测试指南》的预购信息已经发布,可以在京东和当当网进行预购。感谢几位大牛给出的书评,并呼吁大家的支持。明天京东的链接也将发布。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文介绍了C++中省略号类型和参数个数不确定函数参数的使用方法,并提供了一个范例。通过宏定义的方式,可以方便地处理不定参数的情况。文章中给出了具体的代码实现,并对代码进行了解释和说明。这对于需要处理不定参数的情况的程序员来说,是一个很有用的参考资料。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 本文介绍了MyBioSource转甲状腺素蛋白定量检测ELISA试剂盒的应用方法及特点。ELISA法作为一项新技术在免疫诊断中的应用范围不断扩大,不仅适用于多种病原微生物引起的传染病、非传染病的免疫诊断,也可用于大/小分子抗原的定量检测。ELISA法具有灵敏、特异、简单、快速、稳定及易于自动化操作等特点,是一种早期诊断的良好方法,也可用于血清流行病学调查。MyBioSource转甲状腺素蛋白定量检测ELISA试剂盒使用方法包括对血清和血浆的操作要求。 ... [详细]
  • Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
    本文介绍了Android Studio Bumblebee | 2021.1.1(大黄蜂版本)的使用方法和相关知识,包括Gradle的介绍、设备管理器的配置、无线调试、新版本问题等内容。同时还提供了更新版本的下载地址和启动页面截图。 ... [详细]
  • 本文介绍了一种划分和计数油田地块的方法。根据给定的条件,通过遍历和DFS算法,将符合条件的地块标记为不符合条件的地块,并进行计数。同时,还介绍了如何判断点是否在给定范围内的方法。 ... [详细]
  • 本文讲述了作者通过点火测试男友的性格和承受能力,以考验婚姻问题。作者故意不安慰男友并再次点火,观察他的反应。这个行为是善意的玩人,旨在了解男友的性格和避免婚姻问题。 ... [详细]
  • 安卓select模态框样式改变_微软Office风格的多端(Web、安卓、iOS)组件库——Fabric UI...
    介绍FabricUI是微软开源的一套Office风格的多端组件库,共有三套针对性的组件,分别适用于web、android以及iOS,Fab ... [详细]
  • 本文介绍了多因子选股模型在实际中的构建步骤,包括风险源分析、因子筛选和体系构建,并进行了模拟实证回测。在风险源分析中,从宏观、行业、公司和特殊因素四个角度分析了影响资产价格的因素。具体包括宏观经济运行和宏经济政策对证券市场的影响,以及行业类型、行业生命周期和行业政策对股票价格的影响。 ... [详细]
author-avatar
caozhizhao
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有