DSTabBarController

這是一個(gè)tabBarController(導(dǎo)航控制器)組件。簡(jiǎn)單易用,功能強(qiáng)大。輕量、低耦合以及良好的擴(kuò)展性。通過簡(jiǎn)單的幾行代碼可以讓你快速搭建起流行的應(yīng)用UI架構(gòu)。

普通的TabBar

如果你只想構(gòu)建一個(gè)很普通的tabbarController,像這種:

1.gif

那么你只需要這樣做

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.rootViewController = [self rootViewController1];
    [self.window makeKeyAndVisible];
    return YES;
}
// 設(shè)置導(dǎo)航控制器TabBarController
// 傳入一個(gè) `控制器數(shù)組` 和 `選項(xiàng)卡屬性數(shù)組`
- (UIViewController *)rootViewController1
{
    
    NSArray *viewControllers = [self viewControllers];
    NSArray *tabBarItems = [self tabBarItems];
    DSTabBarController *tabBarController = [DSTabBarController tabBarControllerWithViewControllers:viewControllers tabBarItems:tabBarItems];
    return tabBarController;
}

// 這個(gè)數(shù)組是 每個(gè)選項(xiàng)卡上對(duì)應(yīng)的控制器
// 可以傳字符串、Class或者控制器實(shí)例
- (NSArray *)viewControllers
{
     UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:[[DSFirstTableViewController alloc] initWithStyle:UITableViewStylePlain] ];
    return @[nav1,@"DSSecondViewController"];
}
//這是數(shù)組是 每個(gè)選項(xiàng)卡的屬性
//數(shù)組中每個(gè)元素的類型是 `UITabBarItem`
- (NSArray *)tabBarItems
{
    UITabBarItem *tabBarItem1 = [[UITabBarItem alloc] initWithTitle:@"one" image:[UIImage imageNamed:@"my"] selectedImage:[UIImage imageNamed:@"my_h"]];
    UITabBarItem *tabBarItem2 = [[UITabBarItem alloc] initWithTitle:@"two" image:[UIImage imageNamed:@"home"] selectedImage:[UIImage imageNamed:@"home_h"]];
    return @[tabBarItem1,tabBarItem2];
}

DSTabBarController提供了一個(gè)遵守<DSTabBarControllerDelegate>的代理ds_delegate。他可以監(jiān)聽選項(xiàng)卡(實(shí)際上是UITabbarButton)的按下長(zhǎng)按

- (UIViewController *)rootViewController1
{
    NSArray *viewControllers = [self viewControllers];
    NSArray *tabBarItems = [self tabBarItems];
    DSTabBarController *tabBarController = [DSTabBarController tabBarControllerWithViewControllers:viewControllers tabBarItems:tabBarItems];
    //設(shè)置代理
    tabBarController.ds_delegate = self;
    return tabBarController;
}

實(shí)現(xiàn)代理方法

- (void)tabBarController:(DSTabBarController *)tabBarController didClickTabBarButton:(UIControl *)tabBarButton viewController:(UIViewController *)viewController
{
    [self addZoonAnimationWithView:tabBarButton];
}

- (void)addZoonAnimationWithView:(UIView *)view
{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"transform.scale";
    animation.values = @[@1.0,@1.3,@0.9,@1.15,@0.95,@1.02,@1.0];
    animation.duration = 0.9;
    animation.calculationMode = kCAAnimationCubic;
    [view.layer addAnimation:animation forKey:nil];
}

上面代碼是當(dāng)選項(xiàng)卡點(diǎn)擊后,給選項(xiàng)卡添加了一個(gè)幀動(dòng)畫。效果如下:


2.gif

當(dāng)需要監(jiān)聽選項(xiàng)卡長(zhǎng)按時(shí),需要實(shí)現(xiàn)下面的代理方法

//tabBarController默認(rèn)是不能響應(yīng)長(zhǎng)按事件的
//如果需要支持長(zhǎng)按 這里返回YES
- (BOOL)tabBarController:(DSTabBarController *)tabBarController shouldLongPressTabBarButton:(UIControl *)tabBarButton viewController:(UIViewController *)viewController
{
    return YES;
}
//這個(gè)方法里是寫長(zhǎng)按要實(shí)現(xiàn)的功能
- (void)tabBarController:(DSTabBarController *)tabBarController didLongPressUITabBarButton:(UIControl *)tabBarButton viewController:(UIViewController *)viewController
{
  //這里是長(zhǎng)按功能的業(yè)務(wù)代碼
    UIViewController *viewController = tabBarController.publishViewController;
    [UIView animateWithDuration:0.25 animations:^{
        viewController.view.layer.transform = CATransform3DMakeScale(0.95, 0.95, 0.7);
    }];
    UIAlertController *alerController = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [UIView animateWithDuration:0.25 animations:^{
            viewController.view.layer.transform = CATransform3DIdentity;
        }];
     }];
    [alerController addAction:action];
    [viewController presentViewController:alerController animated:YES completion:nil];
}

設(shè)置后的效果如下:


3.gif

你還可以設(shè)置長(zhǎng)按的時(shí)間

@property (nonatomic, assign) CFTimeInterval minimumPressDuration;// Default is 0.8. Time in seconds the fingers must be held down for the gesture to be recognized 

特殊的TabBar

像是閑魚客戶端的那種TabBar,TarBar的中間有個(gè)特殊的按鈕,大致效果如下:


4.gif

這就需要數(shù)據(jù)源方法了,首先設(shè)置數(shù)據(jù)源

 tabBarController.ds_dataSource = self;

實(shí)現(xiàn)以下的數(shù)據(jù)源方法

//設(shè)置特殊按鈕
- (UIButton *)tabBarControllerPublishButton:(DSTabBarController *)tabBarController
{
  return [self publishButton];
}

- (UIButton *)publishButton
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"yuanfenqi"] forState:UIControlStateNormal];[button setImage:[UIImage imageNamed:@"yuanfenqi_h"] forState:UIControlStateSelected];
    button.backgroundColor =[UIColor cyanColor];
    //默認(rèn)`sizeToFit`
    button.frame = CGRectMake(0, 0, 100, 100);
    button.showsTouchWhenHighlighted = YES;
    return button;
}

監(jiān)聽特殊按鈕的點(diǎn)擊

- (void)tabBarController:(DSTabBarController *)tabBarController didClickPublishButton:(UIButton *)button
{
    //添加關(guān)鍵幀動(dòng)畫
    [self addZoonAnimationWithView:button];
    //執(zhí)行按鈕點(diǎn)擊后的事件
    //這里寫你的業(yè)務(wù)邏輯代碼
    UIViewController *vc = [[UIViewController alloc] init];
    vc.view.backgroundColor =[UIColor yellowColor];
    vc.modalPresentationStyle = UIModalPresentationCustom;
    vc.view.alpha = 0.9;
    [tabBarController.selectedViewController presentViewController:vc animated:YES completion:nil];
}
關(guān)于特殊按鈕的位置

若需要指定特殊按鈕的位置,實(shí)現(xiàn)以下數(shù)據(jù)源方法

- (NSUInteger)tabBarControllerPublishButtonIndex:(DSTabBarController *)tabBarController
{
    return 1;
}

若沒有指定特殊按鈕的位置,有如下簡(jiǎn)單的規(guī)則:

如果TabBar上總選項(xiàng)卡個(gè)數(shù)是奇數(shù)個(gè),則特殊按鈕居中;
如果TabBar上總選項(xiàng)卡個(gè)數(shù)是偶數(shù)個(gè),特殊按鈕會(huì)在最后一個(gè)位置。

如果你的應(yīng)用點(diǎn)擊特殊按鈕不是彈出一個(gè)單獨(dú)的控制器,而是根TabBar上的其他選項(xiàng)卡一樣,需要實(shí)現(xiàn)以下數(shù)據(jù)源方法

- (UIViewController *)tabBarControllerSelectPublishButtonViewController:(DSTabBarController *)tabBarController
{
    UIViewController *vc= [[UIViewController alloc] init];
    vc.title  = @"Text";
    vc.view.backgroundColor = [UIColor yellowColor];
    return [[UINavigationController alloc] initWithRootViewController:vc];
}

效果如下圖:

5.gif

監(jiān)聽特殊按鈕的長(zhǎng)按

- (BOOL)tabBarController:(DSTabBarController *)tabBarController shouldLongPressPublishButton:(UIButton *)button
{
    return YES;
}

//這個(gè)方法里是寫長(zhǎng)按要實(shí)現(xiàn)的功能
- (void)tabBarController:(DSTabBarController *)tabBarController didLongPressPublishButton:(UIButton *)button
{
    UIViewController *viewController = tabBarController.publishViewController;
    [UIView animateWithDuration:0.25 animations:^{
        viewController.view.layer.transform = CATransform3DMakeScale(0.95, 0.95, 0.7);
    }];
    UIAlertController *alerController = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [UIView animateWithDuration:0.25 animations:^{
            viewController.view.layer.transform = CATransform3DIdentity;
        }];
        
    }];
    [alerController addAction:action];
    [viewController presentViewController:alerController animated:YES completion:nil];
}

PS:
最低支持:iOS7
支持Pod:Pod 'DSTabBarController'
Demo地址:DSTabBarController

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,690評(píng)論 4 61
  • Fab燕燕閱讀 181評(píng)論 0 0
  • 近輕斷食的概念又被翻出來,雖然但凡有一點(diǎn)營(yíng)養(yǎng)常識(shí)的人肯定對(duì)這種概念嗤之以鼻,但是搭這個(gè)熱點(diǎn)宣傳自己也不乏是一種吸粉...
    健康起飛吧閱讀 207評(píng)論 0 0
  • 畢業(yè)入職以來,使用 SVN 版本控制工具管理代碼,記錄以下對(duì)于工作中較好的使用 SVN 配置; 1. 配置忽略文件...
    dongbingliu閱讀 458評(píng)論 0 1

友情鏈接更多精彩內(nèi)容