测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布最后发布 | 2016年11月 |
SPM支持SPM | ✗ |
maintained by Adam Bardon.
一个使用Swift编写的具有滑动动画的定制TabBar视图。受以下dribble启发。
SlidingTabBar可通过CocoaPods获取。要安装,只需将以下行添加到Podfile中:
pod "SlidingTabBar"
首先创建您的UITabBarViewController类并导入SlidingTabBar
import SlidingTabBar
使其采用SlidingTabBarDataSource,SlidingTabBarDelegate和UITabBarControllerDelegate协议
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中设置好一切
如果要实现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文件。