// // CYLPlusButton.m // CYLTabBarController // // v1.16.0 Created by 微博@iOS程序犭袁 ( http://weibo.com/luohanchenyilong/ ) on 10/20/15. // Copyright © 2015 https://github.com/ChenYilong . All rights reserved. // #import "CYLPlusButton.h" #import "CYLTabBarController.h" #import "UIViewController+CYLTabBarControllerExtention.h" CGFloat CYLPlusButtonWidth = 0.0f; UIButton *CYLExternPlusButton = nil; UIViewController *CYLPlusChildViewController = nil; @implementation CYLPlusButton #pragma mark - #pragma mark - public Methods + (void)registerPlusButton { if (![self conformsToProtocol:@protocol(CYLPlusButtonSubclassing)]) { return; } Class class = self; UIButton *plusButton = [class plusButton]; CYLExternPlusButton = plusButton; CYLPlusButtonWidth = plusButton.frame.size.width; if ([[self class] respondsToSelector:@selector(plusChildViewController)]) { CYLPlusChildViewController = [class plusChildViewController]; if ([[self class] respondsToSelector:@selector(tabBarContext)]) { NSString *tabBarContext = [class tabBarContext]; if (tabBarContext && tabBarContext.length) { [CYLPlusChildViewController cyl_setContext:tabBarContext]; } } else { [CYLPlusChildViewController cyl_setContext:NSStringFromClass([CYLTabBarController class])]; } [[self class] addSelectViewControllerTarget:plusButton]; if ([[self class] respondsToSelector:@selector(indexOfPlusButtonInTabBar)]) { CYLPlusButtonIndex = [[self class] indexOfPlusButtonInTabBar]; } else { [NSException raise:NSStringFromClass([CYLTabBarController class]) format:@"If you want to add PlusChildViewController, you must realizse `+indexOfPlusButtonInTabBar` in your custom plusButton class.【Chinese】如果你想使用PlusChildViewController样式,你必须同时在你自定义的plusButton中实现 `+indexOfPlusButtonInTabBar`,来指定plusButton的位置"]; } } } #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" + (void)registerSubclass { [self registerPlusButton]; } #pragma clang diagnostic pop - (void)plusChildViewControllerButtonClicked:(UIButton *)sender { BOOL notNeedConfigureSelectionStatus = [[self class] respondsToSelector:@selector(shouldSelectPlusChildViewController)] && ![[self class] shouldSelectPlusChildViewController]; if (notNeedConfigureSelectionStatus) { return; } if (sender.selected) { return; } sender.selected = YES; CYLTabBarController *tabBarController = [sender cyl_tabBarController]; NSInteger index = [tabBarController.viewControllers indexOfObject:CYLPlusChildViewController]; @try { [tabBarController setSelectedIndex:index]; } @catch (NSException *exception) { NSLog(@"🔴类名与方法名:%@(在第%@行),描述:%@", @(__PRETTY_FUNCTION__), @(__LINE__), exception); } } #pragma mark - #pragma mark - Private Methods + (void)addSelectViewControllerTarget:(UIButton *)plusButton { id target = self; NSArray *selectorNamesArray = [plusButton actionsForTarget:target forControlEvent:UIControlEventTouchUpInside]; if (selectorNamesArray.count == 0) { target = plusButton; selectorNamesArray = [plusButton actionsForTarget:target forControlEvent:UIControlEventTouchUpInside]; } [selectorNamesArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { SEL selector = NSSelectorFromString(obj); [plusButton removeTarget:target action:selector forControlEvents:UIControlEventTouchUpInside]; }]; [plusButton addTarget:plusButton action:@selector(plusChildViewControllerButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; } /** * 按钮选中状态下点击先显示normal状态的颜色,松开时再回到selected状态下颜色。 * 重写此方法即不会出现上述情况,与 UITabBarButton 相似 */ - (void)setHighlighted:(BOOL)highlighted {} @end