以下筆記內容僅供個人參考,如有理解錯誤,請高抬貴手,仙人指路,互相學習進步...
使用方法教程
使用方法及教程,查看項目源碼github地址:https://github.com/ChenYilong/CYLTabBarController,非常感謝開源的作者,開源促進社區(qū)的發(fā)展,共建和諧社會??!
框架全部文件結構
1.CYLTabBarController
2. CYLTabBar
3.CYLPlusButton
4.UIViewController+CYLTabBarControllerExtention
5.UIView+CYLTabBarControllerExtention
6.UITabBarItem+CYLTabBarControllerExtention
7.UIControl+CYLTabBarControllerExtention
8.CYLConstants
8.總結
解讀CYLTabBarControllerExtention類文件,在源碼中中文注釋自己的理解??
UIViewController+CYLTabBarControllerExtention類文件(.h,.m)
解讀UIViewController+CYLTabBarControllerExtention.h,
UIViewController+CYLTabBarControllerExtention.m
可能需要理解的知識點:
.Block代碼塊 【 http://www.itdecent.cn/p/14efa33b3562】
.類別Category擴展 【http://www.itdecent.cn/p/b49e02eb7eb3】
.runtime機制 【 https://github.com/Tuccuay/RuntimeSummary】
.KVC設計模式 【 http://www.itdecent.cn/p/45cbd324ea65】
.KVO設計模式 【 http://www.itdecent.cn/p/e59bb8f59302】
.異常處理機制 【http://www.itdecent.cn/p/f28b9b3f8e44】
UIViewController+CYLTabBarControllerExtention.h文件
#import <UIKit/UIKit.h>
//定義了三個block代碼塊類型(完成彈出或關閉彈出block)
typedef void (^CYLPopSelectTabBarChildViewControllerCompletion)(__kindof UIViewController *selectedTabBarChildViewController);
typedef void (^CYLPushOrPopCompletionHandler)(BOOL shouldPop,
__kindof UIViewController *viewControllerPopTo,
BOOL shouldPopSelectTabBarChildViewController,
NSUInteger index
);
typedef void (^CYLPushOrPopCallback)(NSArray<__kindof UIViewController *> *viewControllers, CYLPushOrPopCompletionHandler completionHandler);
//給UIViewController添加了一個category分類
@interface UIViewController (CYLTabBarControllerExtention)
//tabbaritem中的badge的視圖view
@property (nonatomic, strong, setter=cyl_setTabBadgePointView:, getter=cyl_tabBadgePointView) UIView *cyl_tabBadgePointView;
//偏移量
@property (nonatomic, assign, setter=cyl_setTabBadgePointViewOffset:, getter=cyl_tabBadgePointViewOffset) UIOffset cyl_tabBadgePointViewOffset;
@property (nonatomic, readonly, getter=cyl_isEmbedInTabBarController) BOOL cyl_embedInTabBarController;
//索引位置index
@property (nonatomic, readonly, getter=cyl_tabIndex) NSInteger cyl_tabIndex;
//tabbar欄的按鈕
@property (nonatomic, readonly) UIControl *cyl_tabButton;
/*!
* @attention
- 調用該方法前已經添加了系統(tǒng)的角標,調用該方法后,系統(tǒng)的角標并未被移除,只是被隱藏,調用 `-cyl_removeTabBadgePoint` 后會重新展示。
- 不支持 CYLPlusChildViewController 對應的 TabBarItem 角標設置,調用會被忽略。
*/
- (void)cyl_showTabBadgePoint;
- (void)cyl_removeTabBadgePoint;
- (BOOL)cyl_isShowTabBadgePoint;
/*!
* Pop 到當前 `NavigationController` 的棧底,并改變 `TabBarController` 的 `selectedViewController` 屬性,并將被選擇的控制器作為返回值返回。
@param index 需要選擇的控制器在 `TabBar` 中的 index。
@return 最終被選擇的控制器。
@attention 注意:方法中的參數(shù)和返回值都是 `UIViewController` 的子類,但并非 `UINavigationController` 的子類。
*/
- (UIViewController *)cyl_popSelectTabBarChildViewControllerAtIndex:(NSUInteger)index;
/*!
* Pop 到當前 `NavigationController` 的棧底,并改變 `TabBarController` 的 `selectedViewController` 屬性,并將被選擇的控制器在 `Block` 回調中返回。
@param index 需要選擇的控制器在 `TabBar` 中的 index。
@attention 注意:方法中的參數(shù)和返回值都是 `UIViewController` 的子類,但并非 `UINavigationController` 的子類。
*/
- (void)cyl_popSelectTabBarChildViewControllerAtIndex:(NSUInteger)index
completion:(CYLPopSelectTabBarChildViewControllerCompletion)completion;
/*!
* Pop 到當前 `NavigationController` 的棧底,并改變 `TabBarController` 的 `selectedViewController` 屬性,并將被選擇的控制器作為返回值返回。
@param classType 需要選擇的控制器所屬的類。
@return 最終被選擇的控制器。
@attention 注意:
- 方法中的參數(shù)和返回值都是 `UIViewController` 的子類,但并非 `UINavigationController` 的子類。
- 如果 TabBarViewController 的 viewControllers 中包含多個相同的 `classType` 類型,會返回最左端的一個。
*/
- (UIViewController *)cyl_popSelectTabBarChildViewControllerForClassType:(Class)classType;
/*!
* Pop 到當前 `NavigationController` 的棧底,并改變 `TabBarController` 的 `selectedViewController` 屬性,并將被選擇的控制器在 `Block` 回調中返回。
@param classType 需要選擇的控制器所屬的類。
@attention 注意:
- 方法中的參數(shù)和返回值都是 `UIViewController` 的子類,但并非 `UINavigationController` 的子類。
- 如果 TabBarViewController 的 viewControllers 中包含多個相同的 `classType` 類型,會返回最左端的一個。
*/
- (void)cyl_popSelectTabBarChildViewControllerForClassType:(Class)classType
completion:(CYLPopSelectTabBarChildViewControllerCompletion)completion;
/*!
*@brief 如果當前的 `NavigationViewController` 棧中包含有準備 Push 到的目標控制器,可以選擇 Pop 而非 Push。
*@param viewController Pop 或 Push 到的“目標控制器”,由 completionHandler 的參數(shù)控制 Pop 和 Push 的細節(jié)。
*@param animated Pop 或 Push 時是否帶動畫
*@param callback 回調,如果傳 nil,將進行 Push。callback 包含以下幾個參數(shù):
*@param viewControllers 表示與“目標控制器”相同類型的控制器;
*@param completionHandler 包含以下幾個參數(shù):
*@param shouldPop 是否 Pop
*@param viewControllerPopTo Pop 回的控制器
*@param shouldPopSelectTabBarChildViewController 在進行 Push 行為之前,是否 Pop 到當前 `NavigationController` 的棧底。
可能的值如下:
NO 如果上一個參數(shù)為 NO,下一個參數(shù) index 將被忽略。
YES 會根據(jù) index 參數(shù)改變 `TabBarController` 的 `selectedViewController` 屬性。
注意:該屬性在 Pop 行為時不起作用。
*@param index Pop 改變 `TabBarController` 的 `selectedViewController` 屬性。
注意:該屬性在 Pop 行為時不起作用。
*/
- (void)cyl_pushOrPopToViewController:(UIViewController *)viewController
animated:(BOOL)animated
callback:(CYLPushOrPopCallback)callback;
/*!
* 如果正要 Push 的頁面與當前棧頂?shù)捻撁骖愋拖嗤瑒t取消 Push
* 這樣做防止主界面卡頓時,導致一個 ViewController 被 Push 多次
*/
- (void)cyl_pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
- (UIViewController *)cyl_getViewControllerInsteadIOfNavigationController;
@end
//異常處理
- (void)checkTabBarChildControllerValidityAtIndex:(NSUInteger)index {
CYLTabBarController *tabBarController = [self cyl_tabBarController];
@try {
UIViewController *viewController;
viewController = tabBarController.viewControllers[index];
UIButton *plusButton = CYLExternPlusButton;
BOOL shouldConfigureSelectionStatus = (CYLPlusChildViewController) && ((index != CYLPlusButtonIndex) && (viewController != CYLPlusChildViewController));
if (shouldConfigureSelectionStatus) {
plusButton.selected = NO;
}
} @catch (NSException *exception) {
NSString *formatString = @"\n\n\
------ BEGIN NSException Log ---------------------------------------------------------------------\n \
class name: %@ \n \
------line: %@ \n \
----reason: The Class Type or the index or its NavigationController you pass in method `-cyl_popSelectTabBarChildViewControllerAtIndex` or `-cyl_popSelectTabBarChildViewControllerForClassType` is not the item of CYLTabBarViewController \n \
------ END ---------------------------------------------------------------------------------------\n\n";
NSString *reason = [NSString stringWithFormat:formatString,
@(__PRETTY_FUNCTION__),
@(__LINE__)];
@throw [NSException exceptionWithName:NSGenericException
reason:reason
userInfo:nil];
}
}
** CYLTabBarControllerExtention總結:**
1、自定義了一個CYLTabBarControllerExtention的視圖category分類,主要實現(xiàn)定義tabbar中的badge提醒視圖以及相關的屬性,以及定義一些操作方法進行視圖選擇,以及選擇完成后的操作執(zhí)行