欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一步一步實現(xiàn)iOS主題皮膚切換效果

 更新時間:2016年09月30日 10:14:13   作者:vbirdbest  
這篇文章主要為大家詳細介紹了一步一步實現(xiàn)iOS主題皮膚切換效果的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了iOS主題皮膚切換代碼,供大家參考,具體內(nèi)容如下

1. 主題皮膚功能切換介紹
主題切換就是根據(jù)用戶設(shè)置不同的主題,來動態(tài)改變用戶的界面,通常會改變navigationBar背景圖片、tabBar背景圖片、tabBar中的按鈕的圖片和選中的背景圖片、navigationItem.title 標(biāo)題的字體顏色、UI中其他元素控件

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

2.項目目錄結(jié)構(gòu)及實現(xiàn)效果截圖




3. 具體實現(xiàn)步驟

1.將image文件夾(group)和 Skins拖入到項目工程中的資源文件夾中
2.創(chuàng)建BaseViewController
3.配置theme.plist
4.事項項目所需的基本框架供能,并實現(xiàn)主題的tableView功能
5.創(chuàng)建主題管理器:ThemeManager
6.自定義ThemeTabBarItem 控件
7.創(chuàng)建UI工廠: UIFactory
8. 實現(xiàn)tableView中的didSelected事件完成主題切換
9.記錄用戶選擇的主題,以便用戶下次啟動時是上次設(shè)置的主題

1.創(chuàng)建BaseViewController

#import <UIKit/UIKit.h> 
@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. 實現(xiàn)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; 
}</span></span> 

<span style="font-weight: normal;"><span style="font-weight: normal;">#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. 創(chuàng)建主題管理器

#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</span></span> 

<span style="font-weight: normal;"><span style="font-weight: normal;">#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 
#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. 創(chuàng)建主題按鈕 ThemeTabBarItem

#import <UIKit/UIKit.h> 
@interface ThemeTabBarItem : UITabBarItem 
 
@property (nonatomic, copy) NSString * imageName; 
@property (nonatomic, copy) NSString * selectedImageName; 
 
 
- (id) initWithTitle:(NSString *)title imageName:(NSString *)imageName selectedImage:(NSString *)selectedImageName; 
 
@end </span></span> 

<span style="font-weight: normal;"><span style="font-weight: normal;">#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;   // 此時會調(diào)用[self setImageName:imageName] ---> [self reloadThemeImage] --->[self setImage:image] 
  self.selectedImageName = selectedImageName;// 此時會調(diào)用[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. 創(chuàng)建UI工廠

#import <Foundation/Foundation.h> 
#import <UIKit/UIKit.h> 
 
@interface UIFactory : NSObject 
 
+ (UITabBarItem *) createTabBarItemWithTitle:(NSString *)title imageName:(NSString *)imageName selectedImage:(NSString *)selectedImageName; 
 
 
@end</span></span> 
<span style="font-weight: normal;"><span style="font-weight: normal;">#import <Foundation/Foundation.h> 
#import <UIKit/UIKit.h> 
 
@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. 實現(xiàn)選中單元格的事件

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

#import "BaseViewController.h" 
 
@interface MineViewController : BaseViewController <UITableViewDelegate, UITableViewDataSource> 
 
 
@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; 
 } 
  
 // 記錄當(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]; 
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS仿微博客戶端一條微博的展示效果

    iOS仿微博客戶端一條微博的展示效果

    這篇文章主要為大家詳細介紹了iOS仿微博客戶端,一條微博的布局,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • iOS 13適配匯總(推薦)

    iOS 13適配匯總(推薦)

    這篇文章主要介紹了iOS 13適配匯總,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • iOS 讀取URL圖片并存儲到本地的實例

    iOS 讀取URL圖片并存儲到本地的實例

    下面小編就為大家?guī)硪黄猧OS 讀取URL圖片并存儲到本地的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • ios中g(shù)etTime()的兼容性實例代碼

    ios中g(shù)etTime()的兼容性實例代碼

    在本篇文章里小編給大家整理的是關(guān)于ios中g(shù)etTime()的兼容性實例代碼,需要的朋友們可以學(xué)習(xí)下。
    2020-03-03
  • ios原生二維碼掃描

    ios原生二維碼掃描

    本文給大家介紹ios原生二維碼掃描,有需要的朋友可以參考下
    2015-09-09
  • iOS實現(xiàn)自動循環(huán)播放的banner實例詳解

    iOS實現(xiàn)自動循環(huán)播放的banner實例詳解

    輪播視圖通常也叫Banner,90%以上App都會用到的一個控件,網(wǎng)上有很多開源代碼,下面這篇文章主要給大家介紹了關(guān)于利用iOS如何實現(xiàn)自動循環(huán)播放的banner的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下。
    2017-12-12
  • IOS 調(diào)整內(nèi)存中的圖片大小實例詳解

    IOS 調(diào)整內(nèi)存中的圖片大小實例詳解

    這篇文章主要介紹了IOS 調(diào)整內(nèi)存中的圖片大小實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • 快速解決低版本Xcode不支持高版本iOS真機調(diào)試的問題方法

    快速解決低版本Xcode不支持高版本iOS真機調(diào)試的問題方法

    這篇文章主要介紹了快速解決低版本Xcode不支持高版本iOS真機調(diào)試的問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • IOS 中XAMPP配置問題及解決方法

    IOS 中XAMPP配置問題及解決方法

    這篇文章主要介紹了IOS 中XAMPP配置問題及解決方法的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • iOS開發(fā)之視圖切換

    iOS開發(fā)之視圖切換

    在iOS開發(fā)中視圖的切換是很頻繁的,獨立的視圖應(yīng)用在實際開發(fā)過程中并不常見,除非你的應(yīng)用足夠簡單。在iOS開發(fā)中常用的視圖切換有三種,今天我們將一一介紹,希望大家能夠喜歡。
    2016-04-04

最新評論