美文网首页
swift创建带动画的Tababr

swift创建带动画的Tababr

作者: 男人宫 | 来源:发表于2020-03-30 09:30 被阅读0次
    class ZZTabBarViewController: UITabBarController {
    
        //定义默认选中的tabarItem
        var indexFlag = 0
        
        override func viewDidLoad() {
            super.viewDidLoad()
            //设置tabbar背景色
             UITabBar.appearance().barTintColor = UIColor.white
            self.addController()
        }
        //初始化VC
        private func addController()
        {
            self.addchildToShow(FirstViewController(), "首页", "Home", "Home_active")
            self.addchildToShow(SecondViewController(), "分类", "Catalog", "Catalog_active")
            self.addchildToShow(ThreeViewController(), "清单", "list", "list_active")
            self.addchildToShow(FourViewController(), "我的", "User", "User_active")
        }
        //设置vc对应的信息
        private  func addchildToShow(_ childvc: UIViewController, _ titleStr: String, _ image: String, _ selectImage: String) {
            //设置对应vc的文字
            childvc.tabBarItem.title = titleStr
            //设置vc未选中的图片
            childvc.tabBarItem.image = UIImage(named: image)?.withRenderingMode(.alwaysOriginal)
            //设置vc选中时候的图片(给什么图片显示什么图片)
            childvc.tabBarItem.selectedImage = UIImage(named: selectImage)?.withRenderingMode(.alwaysOriginal)
            //选中tabbarItem文字颜色
            UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.red], for: .selected)
            //头部导航
            let nav = UINavigationController()
            //将视图加入导航控制器的容器里
            nav.addChild(childvc)
            //将导航加入tabbar的控制器容器
            addChild(nav)
            
        }
    
        //点击item的方法
        
        override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
            if let index = tabBar.items?.firstIndex(of: item)
            {
                if indexFlag != index {
                    self.animationWithIndex(index: index)
                }
            }
        }
        //实现缩放动画
        private func animationWithIndex(index: Int){
             var arrViews = [UIView]()
            for tabbarButton in tabBar.subviews {
                //遍历tabbar上面的bUITabBarButton
                if tabbarButton.isKind(of: NSClassFromString("UITabBarButton")!) {
                    arrViews.append(tabbarButton)
                }
            }
            let animation = CABasicAnimation(keyPath: "transform.scale")
            //动画执行的时间函数
            animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
            //动画执行时间
            animation.duration = 0.3
            //动画重复次数
            animation.repeatCount = 1
            //动画是否恢复原始状态
            animation.autoreverses = true
            //初始key
            animation.fromValue = NSNumber(value: 0.8)
            //结束key
            animation.toValue = NSNumber(value: 1.1)
            //把动画加载到点点击的那个tabbarItem上
            arrViews[index].layer.add(animation, forKey: nil)
            //赋值记录点击的tababrItem
            indexFlag = index
            
        }
    }
    

    相关文章

      网友评论

          本文标题:swift创建带动画的Tababr

          本文链接:https://www.haomeiwen.com/subject/brsmuhtx.html