HKAnimatedTabBar 0.0.5

HKAnimatedTabBar 0.0.5

Kyryl Horbushko 主维护。



  • Kyryl

AnimatedTabBar

轻量级的库,用于替换标准的 UITabBarController,并带有动画和完全自定义的功能。

Swift ICONKit-license Platform type

demo demo

安装

使用 CocoaPods

只需将以下行添加到您的 Podfile 中:

pod 'HKAnimatedTabBar'

这将下载 HKAnimatedTabBar 二进制文件和相关依赖项到 Pods/,在下次执行 pod install 时。

这是安装 CalendarView 特定版本的建议方法。

使用

  • UITabbarController 添加到界面构建器中,并指定类名为 HKAnimatedTabBar,同时更新 UITabBar 的类

demo

或者相反,创建 AnimatedTabBarController 和/或 AnimatedTabBarView 的子类,并将其配置

import UIKit

final class CustomTabBarController: AnimatedTabBarController {

 // MARK: - Override

 override func configure() {
 
 		// set selected index
   selectedIndex = 2 
   tabBar.replaceView.selectedIndex = selectedIndex
   
   	// configure items
   let items = HightLightableTabBarModel.prepareItems()
   configureFor(items)

   	// modify appearence
   tabBar.replaceView.contentContainerShadowColor = UIColor.lightGray
   tabBar.replaceView.contentContainerShadowOpacity = 0.2
   tabBar.replaceView.contentContainerCornerRadius = 18

   tabBar.replaceView.gradientColors = [
     UIColor.white,
     UIColor.white,
     UIColor.gray
   ]
 }
}

下一步 - 使用 AnimatedTabBarItemRepresentable 创建您的动画 TabBarItem。每个按钮都必须有 AnimatedTabBarItemModel 支撑

/*
Represent model for tabBar item

- Tag: 1000
- Version: 0.1
*/
public protocol AnimatedTabBarItemModel {

 /// unique identifier of model
 var identifier: String { get }

 /// return view for model, descibed as AnimatedTabBarItemRepresentable, should be inherited from UIView
 var viewRepresentation: AnimatedTabBarItemRepresentable { get }

 /// describe size of item when selected
 var selectedComponentWidth: CGFloat { get }

 /// action which will be triggered when item selected
 var action: ((AnimatedTabBarItemModel) -> ())? { get set }
}

您可以根据需要添加更多属性来帮助您自定义按钮视图 - 查看示例了解更多。

AnimatedTabBarItemRepresentable 协议表示一个具体的对象(UIView),该对象将被用于 tabBar 上的按钮代替

/*
View that represent object for tabBar button

- Tag: 1001
- Version: 0.1
*/
public protocol AnimatedTabBarItemRepresentable where Self: UIView {

 /// This function called when view is ready to be displayed but not configured yet
 ///  - Parameter item: item that contains current settings for button replacement see [AnimatedTabBarItemModel](x-source-tag://1000)
 func configure(item: AnimatedTabBarItemModel)

 /// This function triggered when user select/deselect tabBar item
 ///  - Parameter selected: indicate current selection state
 ///  - Parameter item: item that contains current settings for button replacement see [AnimatedTabBarItemModel](x-source-tag://1000)
 func switchToSelectedState(_ selected: Bool, item: AnimatedTabBarItemModel)
}

下一步 - 创建项目并配置 AnimatedTabBarController

// if you don't create subclass of tabBarController - in very first controller that used in tabBar do next

class ViewController: UIViewController {

 override func viewDidLoad() {
   super.viewDidLoad()

    // find `AnimatedTabBarController `
   if let tabBarController = self.tabBarController as? AnimatedTabBarController {
   // create items for controller
     let items = ExpandableTabItemModel.prepareItems()
   // set items to controller
     tabBarController.configureFor(items)
   }
   
   // some other setup
 }
}

// if you create subclass of `AnimatedTabBarController`, that perform this configuration in subclass
final class CustomTabBarController: AnimatedTabBarController {

 // MARK: - Override

 override func configure() {
   let items = HightLightableTabBarModel.prepareItems()
   configureFor(items)

   // any other config - check samples for more
 }
}

完成!

构建并运行:)

待办事项

  • 测试

要求

  • Xcode 10 或更高版本
  • iOS 11 或更高版本
  • Swift 5 或更高版本
  • Cocoapods

许可证

MIT 许可证。

联系

如果你对 AnimatedTabBar 有问题或遇到问题,请创建一个问题

如果你想做出贡献,只需创建一个 pull request。