我們雖然已經(jīng)將主骨架拆分了,但是畢竟我們TZSoundMain主骨架里面到底有什么方法,具體方法做什么事情,外界并不知道,如果不把主骨架里面所有的文件看一遍是不可能做到的,這樣對其他使用我們私有庫的人來說相當不方便,所以我們應(yīng)該做一個中間層來統(tǒng)一披露我們的API,讓外界來使用,而且我們沒有在中間層披露的API,其實是不允許外界使用的,但是好處就是一目了然,其他人通過中間層很快就知道怎么去使用我們的私有庫。
-
我們在這里畫一張圖來說明這個問題
這張圖很好說明了這個問題,我們將私有庫里面的內(nèi)容通過中間層披露出公共的API,在此基礎(chǔ)上外界使用的時候只需要調(diào)用中間層的API就可以直接和我們的私有庫的內(nèi)容進行交互,而不需要再苦苦的追尋所需要的內(nèi)容,一目了然。
現(xiàn)在我們以我們的主骨架為例,將TZSoundMain里面的內(nèi)容增加一個中間層用來披露我們提供給外界的API。
-
創(chuàng)建一個類MainModuleAPI
/**
獲取根控制器
@return rootTabBarcontroller
*/
+ (UITabBarController *) rootTabBarController;
/**
添加子控制器
@param viewController 子控制器
@param normalImageName 普通狀態(tài)下地圖片
@param selectedImageName 選中狀態(tài)下地圖片
@param isRequired 是否需要包裝導(dǎo)航控制器
*/
+ (void) addChildViewController: (UIViewController *) viewController normalImageName: (NSString *) normalImageName selectedImageName: (NSString *)selectedImageName isRequiredNavgationController: (BOOL) isRequired;
/**
設(shè)置tabbar中間控件的點擊代碼塊
@param middleClickBlock 點擊代碼塊
*/
+ (void) setTabbarMiddleButtonClick:(void(^)(BOOL isPlaying))middleClickBlock;
/**
設(shè)置全局導(dǎo)航欄標題背景圖片
@param globalImage 全局導(dǎo)航欄背景圖片
*/
+ (void) setNavigationBarGlobalBackGroundImage: (UIImage *) globalImage;
/**
設(shè)置全局導(dǎo)航欄標題顏色, 和文字大小
@param globalTextColor 全局導(dǎo)航欄標題顏色
@param fontSize 全局導(dǎo)航欄文字大小
*/
+ (void) setNavigationBarGlobalTextColor: (UIColor *)globalTextColor andFontSize: (CGFloat) fontSize;
+ (UITabBarController *)rootTabBarController
{
return [TZTabBarController shareInstance];
}
+ (void)addChildViewController:(UIViewController *)viewController normalImageName:(NSString *)normalImageName selectedImageName:(NSString *)selectedImageName isRequiredNavgationController:(BOOL)isRequired
{
[[TZTabBarController shareInstance] addChildVC:viewController normalImageName:normalImageName selectedImageName:selectedImageName isRequiredNavController:isRequired];
}
+ (void)setTabbarMiddleButtonClick:(void (^)(BOOL))middleClickBlock
{
TZTabBar *tabbar = (TZTabBar *)[TZTabBarController shareInstance].tabBar;
tabbar.middleClickBlock = middleClickBlock;
}
+ (void)setNavigationBarGlobalBackGroundImage:(UIImage *)globalImage
{
[TZNavBar setGlobalBackGroundImage:globalImage];
}
+ (void)setNavigationBarGlobalTextColor:(UIColor *)globalTextColor andFontSize:(CGFloat)fontSize
{
[TZNavBar setGlobalTextColor:globalTextColor andFontSize:fontSize];
}
其實我們注意觀察一下可以發(fā)現(xiàn),我們只不過是對之前自定義的tabbarController,tabbar,navbar這些類的方法做了二次封裝,然后根據(jù)需要對外界暴露了我們想要公開的API,這樣做的好處是,不需要再向外界交代太多信息,只需要讓外界和我們的中間層交互就可以使用我們的私有庫里面的內(nèi)容。
- 那么按照我們對私有庫升級的步驟來操作就可以了,首先修改spec文件的版本號,然后提交代碼到我們的遠程代碼倉庫,并且打上tag值
- 我們校驗的時候發(fā)現(xiàn)還是和之前一樣提示我們找不到Category庫,但是不必在意直接提交就可以了,畢竟此時的確找不到私有庫
-
上傳.spec文件到本地索引庫,接著會自動將.spec文件推送到遠程索引庫
-
緊接自然是進入到宿主工程中重新安裝我們的TZSoundMain私有庫,記得要將podfile.lock文件刪除掉
10.png
我們看看宿主工程中有沒有將我們的披露API的中間層安裝進來
- 接下來我們要將appdelegate之前使用加載主骨架的代碼全部重構(gòu)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UITabBarController *rootVC = [MainModuleAPI rootTabBarController];
[MainModuleAPI addChildViewController:[TestVC new] normalImageName:@"tabbar_find_n" selectedImageName:@"tabbar_find_h" isRequiredNavgationController:YES];
[MainModuleAPI addChildViewController:[UIViewController new] normalImageName:@"tabbar_sound_n" selectedImageName:@"tabbar_sound_h" isRequiredNavgationController:YES];
[MainModuleAPI addChildViewController:[UIViewController new] normalImageName:@"tabbar_download_n" selectedImageName:@"tabbar_download_h" isRequiredNavgationController:YES];
[MainModuleAPI addChildViewController:[UIViewController new] normalImageName:@"tabbar_me_n" selectedImageName:@"tabbar_me_h" isRequiredNavgationController:YES];
[MainModuleAPI setTabbarMiddleButtonClick:^(BOOL isPlaying) {
if (isPlaying) {
NSLog(@"播放");
}else {
NSLog(@"暫停");
}
}];
self.window.rootViewController = rootVC;
[self.window makeKeyAndVisible];
return YES;
}
- 那么最后我們看看效果是不是和之前宿主工程一般無二
那么組件化披露API就講完了,到目前為止組件化的基礎(chǔ)就全部講完了,接下來就真正的帶著大家一點點來完善整個項目了。