Skip to content

Commit

Permalink
代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaobs committed Dec 4, 2016
1 parent ecea9db commit 5b8df43
Show file tree
Hide file tree
Showing 17 changed files with 156 additions and 75 deletions.
9 changes: 6 additions & 3 deletions JMTabBarKit/JMTabBarKit/JMTabBarController.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,26 @@ typedef void (^JMTabBarViewWillDisappear) (void);
typedef void (^JMTabBarViewDidAppear) (void);
typedef void (^JMTabBarViewDidDisappear) (void);

typedef BOOL (^JMTabBarLoginStateControl) (UIViewController *viewController, NSInteger selectedIndex);
typedef BOOL (^JMTabBarShouldSelectBlock) (UIViewController *viewController, NSInteger shouldSelectedIndex);

@interface JMTabBarController : UITabBarController<UITabBarControllerDelegate>

@property (nonatomic,copy) JMTabBarViewWillAppear tabBarViewWillAppear;
@property (nonatomic,copy) JMTabBarViewWillDisappear tabBarViewWillDisappear;
@property (nonatomic,copy) JMTabBarViewDidAppear tabBarViewDidAppear;
@property (nonatomic,copy) JMTabBarViewDidDisappear tabBarViewDidDisappear;
/** 登录状态控制(比如未登录跳转到登录)**/
@property (nonatomic,copy) JMTabBarLoginStateControl tabBarLoginStateControl;//
/** 控制TabBarItem能不能选中(可用于如登录控制等) **/
@property (nonatomic,copy) JMTabBarShouldSelectBlock tabBarShouldSelectBlock;
/** tabBar数组,存放每个JMTabBarItem **/
@property (nonatomic,strong) NSArray *tabBarArray;
/** 选中时候的tabBar文本样式 **/
@property (nonatomic,strong) NSDictionary *tabBarSelectedTextAttributesDic;
/** 未选中时候的tabBar文本样式 **/
@property (nonatomic,strong) NSDictionary *tabBarUnSelectedTextAttributesDic;

- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;

/**
* @brief 实例化
*
Expand Down
31 changes: 3 additions & 28 deletions JMTabBarKit/JMTabBarKit/JMTabBarController.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@

#define JM_TabBar_IOS7_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)

NSString *const JM_TabBar_LoginStateControl_Dic_Index = @"Index";
NSString *const JM_TabBar_LoginStateControl_Dic_Obj = @"Obj";

@interface JMTabBarController ()
{
NSUInteger _tagOrigin;
NSMutableArray *_loginStateControlArray;
}
@end

Expand Down Expand Up @@ -89,12 +85,6 @@ - (instancetype)initWithTabBarArray:(NSArray *)tabBarArray
}

- (void)setupTabBarController {
if (_loginStateControlArray == nil) {
_loginStateControlArray = [NSMutableArray array];
}else {
[_loginStateControlArray removeAllObjects];
}

if (_tabBarArray != nil && _tabBarArray.count != 0) {
NSUInteger count = _tabBarArray.count;
NSMutableArray *viewControllerArray = [NSMutableArray array];
Expand Down Expand Up @@ -125,14 +115,6 @@ - (void)setupTabBarController {
[navVC.tabBarItem setTitleTextAttributes:self.tabBarUnSelectedTextAttributesDic forState:UIControlStateNormal];
}

if (item.loginStateControl) {
// 需要登录状态控制
[_loginStateControlArray addObject:@{
JM_TabBar_LoginStateControl_Dic_Index:@(i),
JM_TabBar_LoginStateControl_Dic_Obj:navVC,
}];
}

[viewControllerArray addObject:navVC];
}
}
Expand Down Expand Up @@ -182,16 +164,9 @@ - (void)setTabBarUnSelectedTextAttributesDic:(NSDictionary *)tabBarUnSelectedTex
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
BOOL shouldSelected = YES;

if (_loginStateControlArray != nil && _loginStateControlArray.count != 0) {
for (int i = 0; i < _loginStateControlArray.count; i++) {
NSDictionary *dict = _loginStateControlArray[i];

if (dict[JM_TabBar_LoginStateControl_Dic_Obj] == viewController) {
if (_tabBarLoginStateControl != nil) {
shouldSelected = _tabBarLoginStateControl(dict[JM_TabBar_LoginStateControl_Dic_Obj], [dict[JM_TabBar_LoginStateControl_Dic_Index] integerValue]);
}
}
}
if (_tabBarShouldSelectBlock) {
NSUInteger index = [self.viewControllers indexOfObject:viewController];
shouldSelected = _tabBarShouldSelectBlock(viewController, index);
}

if (shouldSelected) {
Expand Down
20 changes: 12 additions & 8 deletions JMTabBarKit/JMTabBarKit/JMTabBarItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@
@property (nonatomic,strong) NSString *selectedImageName;
@property (nonatomic,strong) NSString *unSelectedImageName;
@property (nonatomic,assign) BOOL selected; // 是否被选中显示
@property (nonatomic,assign) BOOL loginStateControl; // 是否需要登录状态控制

+ (instancetype)tabBarItemWithTitle:(NSString *)title
controllClass:(Class)controllerClass
selectedImageName:(NSString *)selectedImageName
unSelectedImageName:(NSString *)unSelectedImageName
selected:(BOOL)selected;

/**
* @brief TabBarItem
*
* @param title 标题
* @param controllerClass 类
* @param selectedImageName 选中状态显示图片
* @param unSelectedImageName 未选中状态显示图片
* @param selected 是否选中
*
* @return JMTabBarController
*/
+ (instancetype)tabBarItemWithTitle:(NSString *)title
controllClass:(Class)controllerClass
selectedImageName:(NSString *)selectedImageName
unSelectedImageName:(NSString *)unSelectedImageName
selected:(BOOL)selected
loginStateControl:(BOOL)loginStateControl;
selected:(BOOL)selected;

@end
27 changes: 11 additions & 16 deletions JMTabBarKit/JMTabBarKit/JMTabBarItem.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,33 @@ - (instancetype)init
_selectedImageName = @"";
_unSelectedImageName = @"";
_selected = false;
_loginStateControl = false;
}

return self;
}

/**
* @brief TabBarItem
*
* @param title 标题
* @param controllerClass 类
* @param selectedImageName 选中状态显示图片
* @param unSelectedImageName 未选中状态显示图片
* @param selected 是否选中
*
* @return JMTabBarController
*/
+ (instancetype)tabBarItemWithTitle:(NSString *)title
controllClass:(Class)controllerClass
selectedImageName:(NSString *)selectedImageName
unSelectedImageName:(NSString *)unSelectedImageName
selected:(BOOL)selected {
return [JMTabBarItem tabBarItemWithTitle:title
controllClass:controllerClass
selectedImageName:selectedImageName
unSelectedImageName:unSelectedImageName
selected:selected
loginStateControl:NO];
}

+ (instancetype)tabBarItemWithTitle:(NSString *)title
controllClass:(Class)controllerClass
selectedImageName:(NSString *)selectedImageName
unSelectedImageName:(NSString *)unSelectedImageName
selected:(BOOL)selected
loginStateControl:(BOOL)loginStateControl {
JMTabBarItem *item = [[JMTabBarItem alloc] init];
item.title = title;
item.controllerClass = controllerClass;
item.selectedImageName = selectedImageName;
item.unSelectedImageName = unSelectedImageName;
item.selected = selected;
item.loginStateControl = loginStateControl;

return item;
}
Expand Down
8 changes: 8 additions & 0 deletions JMTabBarKitTest.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
1A2AC5551AE9D5FF00F7E0A2 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 1A2AC5531AE9D5FF00F7E0A2 /* Main.storyboard */; };
1A2AC5571AE9D5FF00F7E0A2 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 1A2AC5561AE9D5FF00F7E0A2 /* Images.xcassets */; };
1A2AC55A1AE9D5FF00F7E0A2 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 1A2AC5581AE9D5FF00F7E0A2 /* LaunchScreen.xib */; };
1A658B861DF4629600E265A7 /* 1.pdf in Resources */ = {isa = PBXBuildFile; fileRef = 1A658B841DF4629600E265A7 /* 1.pdf */; };
1A658B871DF4629600E265A7 /* 2.pdf in Resources */ = {isa = PBXBuildFile; fileRef = 1A658B851DF4629600E265A7 /* 2.pdf */; };
1AA540821AEF2C6A0094A70F /* FirstViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 1AA540811AEF2C6A0094A70F /* FirstViewController.m */; };
1AA540861AEF2CB90094A70F /* SecondViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 1AA540851AEF2CB90094A70F /* SecondViewController.m */; };
1AA540891AEF2D070094A70F /* ThirdViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 1AA540881AEF2D070094A70F /* ThirdViewController.m */; };
Expand Down Expand Up @@ -65,6 +67,8 @@
1A2AC5561AE9D5FF00F7E0A2 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
1A2AC5591AE9D5FF00F7E0A2 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = "<group>"; };
1A2AC5921AE9D63900F7E0A2 /* JMTabBarKit.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = JMTabBarKit.xcodeproj; path = JMTabBarKit/JMTabBarKit.xcodeproj; sourceTree = "<group>"; };
1A658B841DF4629600E265A7 /* 1.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = 1.pdf; sourceTree = "<group>"; };
1A658B851DF4629600E265A7 /* 2.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = 2.pdf; sourceTree = "<group>"; };
1AA540801AEF2C6A0094A70F /* FirstViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FirstViewController.h; sourceTree = "<group>"; };
1AA540811AEF2C6A0094A70F /* FirstViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FirstViewController.m; sourceTree = "<group>"; };
1AA540841AEF2CB90094A70F /* SecondViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SecondViewController.h; sourceTree = "<group>"; };
Expand All @@ -88,6 +92,8 @@
1A1664981C7819A700D5151B /* image */ = {
isa = PBXGroup;
children = (
1A658B841DF4629600E265A7 /* 1.pdf */,
1A658B851DF4629600E265A7 /* 2.pdf */,
1A1664991C7819A700D5151B /* Tab_Assistant_Highlight.png */,
1A16649A1C7819A700D5151B /* [email protected] */,
1A16649B1C7819A700D5151B /* Tab_Assistant_Normal.png */,
Expand Down Expand Up @@ -235,6 +241,7 @@
files = (
1A1664B21C7819A700D5151B /* [email protected] in Resources */,
1A1664B71C7819A700D5151B /* Tab_Mine_Normal.png in Resources */,
1A658B871DF4629600E265A7 /* 2.pdf in Resources */,
1A1664B11C7819A700D5151B /* Tab_Main_Highlight.png in Resources */,
1A1664B31C7819A700D5151B /* Tab_Main_Normal.png in Resources */,
1A1664B41C7819A700D5151B /* [email protected] in Resources */,
Expand All @@ -243,6 +250,7 @@
1A1664AC1C7819A700D5151B /* [email protected] in Resources */,
1A2AC5551AE9D5FF00F7E0A2 /* Main.storyboard in Resources */,
1A1664A91C7819A700D5151B /* Tab_Assistant_Highlight.png in Resources */,
1A658B861DF4629600E265A7 /* 1.pdf in Resources */,
1A2AC55A1AE9D5FF00F7E0A2 /* LaunchScreen.xib in Resources */,
1A1664B61C7819A700D5151B /* [email protected] in Resources */,
1A2AC5571AE9D5FF00F7E0A2 /* Images.xcassets in Resources */,
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "JMTabBarKit/JMTabBarKit/JMTabBarController.m"
timestampString = "493203903.896658"
timestampString = "502549470.992177"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "102"
endingLineNumber = "102"
startingLineNumber = "92"
endingLineNumber = "92"
landmarkName = "-setupTabBarController"
landmarkType = "7">
</BreakpointContent>
Expand All @@ -42,12 +42,28 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "JMTabBarKit/JMTabBarKit/JMTabBarController.m"
timestampString = "493203903.896658"
timestampString = "502549470.992177"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "138"
endingLineNumber = "138"
landmarkName = "-setTabBarSelectedIndex:"
startingLineNumber = "120"
endingLineNumber = "120"
landmarkName = "-setupTabBarController"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "JMTabBarKit/JMTabBarKit/JMTabBarController.m"
timestampString = "502549470.992177"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "86"
endingLineNumber = "86"
landmarkName = "-setupTabBarController"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
Expand All @@ -58,11 +74,11 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "JMTabBarKit/JMTabBarKit/JMTabBarController.m"
timestampString = "493203903.896658"
timestampString = "502555586.434603"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "90"
endingLineNumber = "90"
startingLineNumber = "96"
endingLineNumber = "96"
landmarkName = "-setupTabBarController"
landmarkType = "7">
</BreakpointContent>
Expand Down
22 changes: 12 additions & 10 deletions JMTabBarKitTest/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ - (void)setupJMTabBar {

JMTabBarItem *item2 = [JMTabBarItem tabBarItemWithTitle:@"客服"
controllClass:[SecondViewController class]
selectedImageName:@"Tab_Assistant_Highlight"
selectedImageName:@"Tab_Center_Selected"
unSelectedImageName:@"Tab_Assistant_Normal"
selected:NO];

Expand All @@ -70,8 +70,7 @@ - (void)setupJMTabBar {
controllClass:[ThirdViewController class]
selectedImageName:@"Tab_Mine_Highlight"
unSelectedImageName:@"Tab_Mine_Normal"
selected:NO
loginStateControl:true];
selected:NO];

[tabArray addObject:item3];

Expand All @@ -85,16 +84,19 @@ - (void)setupJMTabBar {
unSelectedTextAttributes:unSelectedStyleDic];

__weak JMTabBarController *weakTabBarVC = _tabBarVC;
_tabBarVC.tabBarLoginStateControl = ^(UIViewController* viewController,NSInteger index) {
ViewController *login = [[ViewController alloc] init];
[weakTabBarVC presentViewController:login animated:YES completion:^{
_tabBarVC.tabBarShouldSelectBlock = ^(UIViewController * viewController,NSInteger shouldSelectedIndex) {
if (shouldSelectedIndex == 2) {
ViewController *login = [[ViewController alloc] init];
[weakTabBarVC presentViewController:login animated:YES completion:^{

}];

}];

return NO;
return NO;
}

return YES;
};
self.window.rootViewController = _tabBarVC;

// UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 5)];
// view.backgroundColor = [UIColor redColor];
// [tabBarVC.tabBar addSubview:view];
Expand Down
5 changes: 5 additions & 0 deletions JMTabBarKitTest/FirstViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ - (void)viewDidLoad {
[btn setTitle:@"切换" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(clickBtn) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 220, 20, 20)];
imageView.image = [UIImage imageNamed:@"Tab_Center_Selected"];

[self.view addSubview:imageView];
}

- (void)didReceiveMemoryWarning {
Expand Down
25 changes: 25 additions & 0 deletions JMTabBarKitTest/Images.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
{
"images" : [
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "29x29",
Expand Down Expand Up @@ -30,6 +40,16 @@
"size" : "60x60",
"scale" : "3x"
},
{
"idiom" : "ipad",
"size" : "20x20",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "29x29",
Expand Down Expand Up @@ -59,6 +79,11 @@
"idiom" : "ipad",
"size" : "76x76",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "83.5x83.5",
"scale" : "2x"
}
],
"info" : {
Expand Down
6 changes: 6 additions & 0 deletions JMTabBarKitTest/Images.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading

0 comments on commit 5b8df43

Please sign in to comment.