SlidingTabBar 0.2.1

SlidingTabBar 0.2.1

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最后发布2016年11月
SPM支持SPM

maintained by Adam Bardon.



SlidingTabBar

一个使用Swift编写的具有滑动动画的定制TabBar视图。受以下dribble启发。

还可以在我的博客中阅读如何实现 - 第1部分第2部分

Animation

要求

  • iOS 8.0+
  • Xcode 7.2+
  • Swift 2+

安装

SlidingTabBar可通过CocoaPods获取。要安装,只需将以下行添加到Podfile中:

pod "SlidingTabBar"

用法

首先创建您的UITabBarViewController类并导入SlidingTabBar

import SlidingTabBar

使其采用SlidingTabBarDataSourceSlidingTabBarDelegateUITabBarControllerDelegate协议

class YourViewController: UITabBarController, SlidingTabBarDataSource, SlidingTabBarDelegate, UITabBarControllerDelegate

viewDidLoad()中设置默认的UITabBar为隐藏,设置selectedIndex为所需值并设置代理。

self.tabBar.hidden = true
self.selectedIndex = 1
self.delegate = self

创建类变量
var tabBarView: SlidingTabBar!
var fromIndex: Int!
var toIndex: Int!

现在在viewDidLoad()中初始化tabBarView

// use default UITabBar's frame or whatever you want
// number of selectedTabBarItemColors has to match number of your tab bar items

tabBarView = SlidingTabBar(frame: self.tabBar.frame, initialTabBarItemIndex: self.selectedIndex)
tabBarView.tabBarBackgroundColor = UIColor.black()
tabBarView.tabBarItemTintColor = UIColor.gray()
tabBarView.selectedTabBarItemTintColor = UIColor.white()
tabBarView.selectedTabBarItemColors = [UIColor.red(), UIColor.green(), UIColor.blue()]
tabBarView.slideAnimationDuration = 0.6
tabBarView.datasource = self
tabBarView.delegate = self
tabBarView.setup()

self.view.addSubview(tabBarView)

实现这些代理和数据源方法

// MARK: - SlidingTabBarDataSource

func tabBarItemsInSlidingTabBar(tabBarView: SlidingTabBar) -> [UITabBarItem] {
    return tabBar.items!
}

// MARK: - SlidingTabBarDelegate

func didSelectViewController(tabBarView: SlidingTabBar, atIndex index: Int, from: Int) {
    self.fromIndex = from
    self.toIndex = index
    self.selectedIndex = index
}

// MARK: - UITabBarControllerDelegate

func tabBarController(tabBarController: UITabBarController, animationControllerForTransitionFromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {

    // use same duration as for tabBarView.slideAnimationDuration
    // you can choose direction in which view controllers should be changed:
    // - .Both(default),
    // - .Reverse,
    // - .Left,
    // - .Right 
    return SlidingTabAnimatedTransitioning(transitionDuration: 0.6, direction: .Both,
     fromIndex: self.fromIndex, toIndex: self.toIndex)
}

最后在Storyboard中设置好一切

  1. 将本地的UITabBarController添加到Storyboard中,与其视图控制器建立关系。
  2. 将您的ViewController作为UITabBarController的定制类。
  3. 设置所有标签栏项目的图片

Imgur

如果要实现3d touch - Home Screen Quick Actions,添加以下内容到AppDelegate

func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {

    // whatever type you have
    if shortcutItem.type == UIApplicationShortcutItem.type { 
        let tabBarController = (window?.rootViewController as! YourViewController)
        tabBarController.selectedIndex = 1 // whatever view controller you need
        tabBarController.tabBarView.initialTabBarItemIndex = tabBarController.selectedIndex
        tabBarController.tabBarView.reloadTabBarView()

        completionHandler(true)
    }

    completionHandler(false)
}

尽情享受! :)

作者

Adam Bardon,[email protected]@bardonadam

许可证

SlidingTabBar采用MIT许可证提供。有关更多信息,请参阅LICENSE文件。