以下笔记内容仅供个人参考,如有理解错误,请高抬贵手,仙人指路,互相学习进步...
使用方法教程
使用方法及教程,查看项目源码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.总结
解读UITabBarItem+CYLTabBarControllerExtention类文件,在源码中中文注释自己的理解👇
UITabBarItem+CYLTabBarControllerExtention类文件(.h,.m)
解读UITabBarItem+CYLTabBarControllerExtention.h,
UITabBarItem+CYLTabBarControllerExtention.m
可能需要理解的知识点:
.Block代码块 【 //www.greatytc.com/p/14efa33b3562】
.类别Category扩展 【//www.greatytc.com/p/b49e02eb7eb3】
.runtime机制 【 https://github.com/Tuccuay/RuntimeSummary】
.KVC设计模式 【 //www.greatytc.com/p/45cbd324ea65】
.KVO设计模式 【 //www.greatytc.com/p/e59bb8f59302】
.异常处理机制 【//www.greatytc.com/p/f28b9b3f8e44】
UITabBarItem+CYLTabBarControllerExtention文件
#import <UIKit/UIKit.h>
@interface UITabBarItem (CYLTabBarControllerExtention)
//添加了一个tabbarButton
@property (nonatomic, readonly) UIControl *cyl_tabButton;
@end
#import "UITabBarItem+CYLTabBarControllerExtention.h"
#import <objc/runtime.h>
#import "UIControl+CYLTabBarControllerExtention.h"
@implementation UITabBarItem (CYLTabBarControllerExtention)
//设置badge值
- (void)cyl_setBadgeValue:(NSString *)badgeValue {
[self.cyl_tabButton cyl_removeTabBadgePoint];
[self cyl_setBadgeValue:badgeValue];
}
//获取badge视图
- (UIControl *)cyl_tabButton {
UIControl *control = [self valueForKey:@"view"];
return control;
}
//runtime方法交换
+ (void)load {
[self cyl_swizzleSetBadgeValue];
}
+ (void)cyl_swizzleSetBadgeValue {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
cyl_ClassMethodSwizzle([self class], @selector(setBadgeValue:), @selector(cyl_setBadgeValue:));
});
}
#pragma mark - private method
BOOL cyl_ClassMethodSwizzle(Class aClass, SEL originalSelector, SEL swizzleSelector) {
Method originalMethod = class_getInstanceMethod(aClass, originalSelector);
Method swizzleMethod = class_getInstanceMethod(aClass, swizzleSelector);
BOOL didAddMethod = class_addMethod(aClass,originalSelector,method_getImplementation(swizzleMethod),method_getTypeEncoding(swizzleMethod));
if (didAddMethod) {
class_replaceMethod(aClass,
swizzleSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzleMethod);
}
return YES;
}
@end
UITabbarController总结:
1、自定义了一个UITabBarItem的分类,实现setBadgeValue与cyl_setBadgeValue方法交换