DSTabBarController

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

普通的TabBar

如果你只想構(gòu)建一個很普通的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;
}
// 設置導航控制器TabBarController
// 傳入一個 `控制器數(shù)組` 和 `選項卡屬性數(shù)組`
- (UIViewController *)rootViewController1
{
    
    NSArray *viewControllers = [self viewControllers];
    NSArray *tabBarItems = [self tabBarItems];
    DSTabBarController *tabBarController = [DSTabBarController tabBarControllerWithViewControllers:viewControllers tabBarItems:tabBarItems];
    return tabBarController;
}

// 這個數(shù)組是 每個選項卡上對應的控制器
// 可以傳字符串、Class或者控制器實例
- (NSArray *)viewControllers
{
     UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:[[DSFirstTableViewController alloc] initWithStyle:UITableViewStylePlain] ];
    return @[nav1,@"DSSecondViewController"];
}
//這是數(shù)組是 每個選項卡的屬性
//數(shù)組中每個元素的類型是 `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提供了一個遵守<DSTabBarControllerDelegate>的代理ds_delegate。他可以監(jiān)聽選項卡(實際上是UITabbarButton)的按下長按。

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

實現(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];
}

上面代碼是當選項卡點擊后,給選項卡添加了一個幀動畫。效果如下:


2.gif

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

//tabBarController默認是不能響應長按事件的
//如果需要支持長按 這里返回YES
- (BOOL)tabBarController:(DSTabBarController *)tabBarController shouldLongPressTabBarButton:(UIControl *)tabBarButton viewController:(UIViewController *)viewController
{
    return YES;
}
//這個方法里是寫長按要實現(xiàn)的功能
- (void)tabBarController:(DSTabBarController *)tabBarController didLongPressUITabBarButton:(UIControl *)tabBarButton viewController:(UIViewController *)viewController
{
  //這里是長按功能的業(yè)務代碼
    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];
}

設置后的效果如下:


3.gif

你還可以設置長按的時間

@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的中間有個特殊的按鈕,大致效果如下:


4.gif

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

 tabBarController.ds_dataSource = self;

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

//設置特殊按鈕
- (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];
    //默認`sizeToFit`
    button.frame = CGRectMake(0, 0, 100, 100);
    button.showsTouchWhenHighlighted = YES;
    return button;
}

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

- (void)tabBarController:(DSTabBarController *)tabBarController didClickPublishButton:(UIButton *)button
{
    //添加關鍵幀動畫
    [self addZoonAnimationWithView:button];
    //執(zhí)行按鈕點擊后的事件
    //這里寫你的業(yè)務邏輯代碼
    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];
}
關于特殊按鈕的位置

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

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

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

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

如果你的應用點擊特殊按鈕不是彈出一個單獨的控制器,而是根TabBar上的其他選項卡一樣,需要實現(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)聽特殊按鈕的長按

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

//這個方法里是寫長按要實現(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)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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

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

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