美文网首页
CYLTabBarController 第四篇(Tabbar底部

CYLTabBarController 第四篇(Tabbar底部

作者: baiwulong | 来源:发表于2017-11-27 10:16 被阅读271次

以下笔记内容仅供个人参考,如有理解错误,请高抬贵手,仙人指路,互相学习进步...

使用方法教程

使用方法及教程,查看项目源码github地址:https://github.com/ChenYilong/CYLTabBarController,非常感谢开源的作者,开源促进社区的发展,共建和谐社会😆!

框架全部文件结构

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.jianshu.com/p/14efa33b3562
.类别Category扩展http://www.jianshu.com/p/b49e02eb7eb3
.runtime机制https://github.com/Tuccuay/RuntimeSummary
.KVC设计模式http://www.jianshu.com/p/45cbd324ea65
.KVO设计模式http://www.jianshu.com/p/e59bb8f59302
.异常处理机制http://www.jianshu.com/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 
   - 调用该方法前已经添加了系统的角标,调用该方法后,系统的角标并未被移除,只是被隐藏,调用 `-cyl_removeTabBadgePoint` 后会重新展示。
   - 不支持 CYLPlusChildViewController 对应的 TabBarItem 角标设置,调用会被忽略。
 */
- (void)cyl_showTabBadgePoint;

- (void)cyl_removeTabBadgePoint;

- (BOOL)cyl_isShowTabBadgePoint;

/*!
 * Pop 到当前 `NavigationController` 的栈底,并改变 `TabBarController` 的 `selectedViewController` 属性,并将被选择的控制器作为返回值返回。
 @param index 需要选择的控制器在 `TabBar` 中的 index。
 @return 最终被选择的控制器。
 @attention 注意:方法中的参数和返回值都是 `UIViewController` 的子类,但并非 `UINavigationController` 的子类。
 */
- (UIViewController *)cyl_popSelectTabBarChildViewControllerAtIndex:(NSUInteger)index;

/*!
 * Pop 到当前 `NavigationController` 的栈底,并改变 `TabBarController` 的 `selectedViewController` 属性,并将被选择的控制器在 `Block` 回调中返回。
 @param index 需要选择的控制器在 `TabBar` 中的 index。
 @attention 注意:方法中的参数和返回值都是 `UIViewController` 的子类,但并非 `UINavigationController` 的子类。
 */
- (void)cyl_popSelectTabBarChildViewControllerAtIndex:(NSUInteger)index
                                           completion:(CYLPopSelectTabBarChildViewControllerCompletion)completion;

/*!
 * Pop 到当前 `NavigationController` 的栈底,并改变 `TabBarController` 的 `selectedViewController` 属性,并将被选择的控制器作为返回值返回。
 @param classType 需要选择的控制器所属的类。
 @return 最终被选择的控制器。
 @attention 注意:
                - 方法中的参数和返回值都是 `UIViewController` 的子类,但并非 `UINavigationController` 的子类。
                - 如果 TabBarViewController 的 viewControllers 中包含多个相同的 `classType` 类型,会返回最左端的一个。
 
 */
- (UIViewController *)cyl_popSelectTabBarChildViewControllerForClassType:(Class)classType;

/*!
 * Pop 到当前 `NavigationController` 的栈底,并改变 `TabBarController` 的 `selectedViewController` 属性,并将被选择的控制器在 `Block` 回调中返回。
 @param classType 需要选择的控制器所属的类。
 @attention 注意:
                - 方法中的参数和返回值都是 `UIViewController` 的子类,但并非 `UINavigationController` 的子类。
                - 如果 TabBarViewController 的 viewControllers 中包含多个相同的 `classType` 类型,会返回最左端的一个。
 */
- (void)cyl_popSelectTabBarChildViewControllerForClassType:(Class)classType
                                                completion:(CYLPopSelectTabBarChildViewControllerCompletion)completion;

/*!
 *@brief 如果当前的 `NavigationViewController` 栈中包含有准备 Push 到的目标控制器,可以选择 Pop 而非 Push。
 *@param viewController Pop 或 Push 到的“目标控制器”,由 completionHandler 的参数控制 Pop 和 Push 的细节。
 *@param animated Pop 或 Push 时是否带动画
 *@param callback 回调,如果传 nil,将进行 Push。callback 包含以下几个参数:
                  *@param viewControllers 表示与“目标控制器”相同类型的控制器;
                  *@param completionHandler 包含以下几个参数:
                                            *@param shouldPop 是否 Pop
                                            *@param viewControllerPopTo Pop 回的控制器
                                            *@param shouldPopSelectTabBarChildViewController 在进行 Push 行为之前,是否 Pop 到当前 `NavigationController` 的栈底。
                                                                                             可能的值如下:
                                                                                             NO 如果上一个参数为 NO,下一个参数 index 将被忽略。
                                                                                             YES 会根据 index 参数改变 `TabBarController` 的 `selectedViewController` 属性。
                                                                                             注意:该属性在 Pop 行为时不起作用。
                                             *@param index Pop 改变 `TabBarController` 的 `selectedViewController` 属性。
                                                           注意:该属性在 Pop 行为时不起作用。
*/
- (void)cyl_pushOrPopToViewController:(UIViewController *)viewController
                             animated:(BOOL)animated
                             callback:(CYLPushOrPopCallback)callback;

/*!
 * 如果正要 Push 的页面与当前栈顶的页面类型相同则取消 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分类,主要实现定义tabbar中的badge提醒视图以及相关的属性,以及定义一些操作方法进行视图选择,以及选择完成后的操作执行

相关文章

网友评论

      本文标题:CYLTabBarController 第四篇(Tabbar底部

      本文链接:https://www.haomeiwen.com/subject/ksyrbxtx.html