Swift3.0 TabbarController封装并添加动画效果

做TabbarController简单封装,并添加点击动画效果

import UIKit

class HPTabbarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.delegate = self // 指定代理  --> extension实现代理方法
        self.view.backgroundColor = UIColor.white
        let tabBar = UITabBar.appearance()
          tabBar.tintColor = UIColor(red: 245 / 255, green: 90 / 255, blue: 93 / 255, alpha: 1/0)
        self.addChildViewControllers()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    private func addChildViewControllers() { // 如果使用文字和切图放在一起的话
        addChildViewController(childController: HomeViewController(), title: "首页", imageName: "homeUnselected", selectedImage: "homeSelected")
        addChildViewController(childController: VideoViewController(), title: "视频", imageName: "videoUnselected", selectedImage: "videoSelected")
        addChildViewController(childController: MineViewController(), title: "我", imageName: "mineUnselected", selectedImage: "mineSelected")
    }

    private func addChildViewController(childController: UIViewController, title:String, imageName: String, selectedImage: String) {
        childController.tabBarItem.image = UIImage(named: imageName)
        childController.tabBarItem.selectedImage = UIImage(named:selectedImage)
        childController.title = title
       // childController.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0) // 如果文字和UI切图在一起  就将title设置为空字符串
        let navController = HPNavigationController(rootViewController: childController)
        addChildViewController(navController)
    }
}

extension HPTabbarController : UITabBarControllerDelegate {

    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {

        if let index = tabBar.items?.index(of: item) {
            tabbarButtonClick(index: index)
            // 发送通知用来监听tabbar点击事件
           // NotificationCenter.default.post(name: NSNotification.Name(rawValue: "TabBarDidSelect"), object: self, userInfo: ["info": index])
        }
    }

    func tabbarButtonClick(index: Int) {
        var arr = [UIView]()
        for tabBarButton in tabBar.subviews {
            if tabBarButton.isKind(of: NSClassFromString("UITabBarButton")!) {
                arr.append(tabBarButton)
            }
        }
        let animation = CABasicAnimation(keyPath: "transform.scale")
        animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) // 速度控制函数
        animation.duration = 0.15
        animation.fromValue = NSNumber(value: 0.7) 
        animation.toValue = NSNumber(value: 1.2)
        arr[index].layer.add(animation, forKey: nil)
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值