RAMAnimatedTabBarController在swif

2019-02-22  本文已影响0人  专心致志的程序员

话不多说,我们直接开始。首先我们用cocopods安装一下我们这个框架。cocopods的使用就不多说了,大家可以自行百度。
然后我们创建一个controller继承我们RAMAnimatedTabBarController。当然不要忘记引入RAMAnimatedTabBarController不然可能会找不到。
接下来我们创建一个类继承至RAMFumeAnimation这样我么可以在这里面重写一下这几个方法

 override func playAnimation(_ icon: UIImageView, textLabel: UILabel) {
        playBounceAnimation(icon)
        textLabel.textColor = CGREEN
        icon.image = UIImage.init(named: "tab_icon_box_pre")
    }
    
    override func deselectAnimation(_ icon: UIImageView, textLabel: UILabel, defaultTextColor: UIColor, defaultIconColor: UIColor) {
        textLabel.textColor = UIColor.withHex("#999999")
        icon.image = UIImage.init(named: "tab_icon_box_nor")
    }
    
    override func selectedState(_ icon: UIImageView, textLabel: UILabel) {
        textLabel.textColor = CGREEN
        icon.image = UIImage.init(named: "tab_icon_box_pre")
        
    }
    
    func playBounceAnimation(_ icon : UIImageView) {
        
        let bounceAnimation = CAKeyframeAnimation(keyPath: "transform.scale")
        bounceAnimation.values = [1.0 ,1.4, 0.9, 1.15, 0.95, 1.02, 1.0]
        bounceAnimation.duration = TimeInterval(duration)
        bounceAnimation.calculationMode = CAAnimationCalculationMode.cubic
        
        icon.layer.add(bounceAnimation, forKey: "bounceAnimation")
        
//        if let iconImage = icon.image {
//            let renderImage = iconImage.withRenderingMode(.alwaysTemplate)
//            icon.image = renderImage
//            icon.tintColor = iconSelectedColor
//        }
    }

这样我们就做好tabbar的点击选中时字体颜色和选中的图片,(PS:我开始的时候我的tabbar的颜色一直是蓝色,就是默认的颜色,需要在这里设置我们选中图片时的颜色)。做好这里后我们回到controller里面。这里很重要的一点我们在加载的时候加载顺序很重要

   override func viewDidLoad() {
       
       addAllChildsControllors();
        super.viewDidLoad()
        commitInitView()
    
    }

👆是两个加载的方法。我们现在只需要添加我们的childVC就好了具体代码
👇的代码里面我们可能要添加多个tabbar并且给不同的tabbar设置不同的动画,这时候我们只需要创建多个集成至RAMFumeAnimation的类就好。

func addAllChildsControllors() {
        addOneChildVC(childVC:JWellFindsGoodsViewController(), title:"找货源", imageNormal:UIImage(imageLiteralResourceName:"tab_icon_box_nor"), imageSelect: UIImage(named:"tab_icon_box_pre"))
        addTwoChildVC(childVC:JWellSchedulingViewController(), title:"调度管理", imageNormal:UIImage(imageLiteralResourceName:"tab_icon_dispatch_nor"), imageSelect: UIImage(named:"tab_icon_dispatch_pre"))
        addThreeChildVC(childVC:JWellWaybillViewController(), title:"运单管理", imageNormal:UIImage(imageLiteralResourceName:"tab_icon_list_nor"), imageSelect: UIImage(named:"tab_icon_list_pre"))
        addFourChildVC(childVC:JWellPersonViewController(), title:"我的", imageNormal:UIImage(imageLiteralResourceName:"tab_icon_mine_nor"), imageSelect: UIImage(named:"tab_icon_mine_pre"))
    }
    
    ///添加一个控制器
    func addOneChildVC(childVC: UIViewController, title: String?, imageNormal: UIImage?, imageSelect:UIImage?) {
        let navVC = UINavigationController(rootViewController: childVC)
        let item = RAMAnimatedTabBarItem(title: title, image: imageNormal, selectedImage: imageSelect)
        let animation = JWellTabbarAnimation()
        item.animation = animation
        item.textColor = UIColor.withHex("#999999")
        item.iconView?.icon.image = imageSelect
        addChild(navVC)
        navVC.tabBarItem = item
    }

然后我们在设置一下我们tabbar的上面的一些基本属性

  func commitInitView() {
        view.backgroundColor = UIColor.white
        tabBar.isTranslucent = false
        tabBar.tintColor = UIColor.white
        tabBar.barTintColor = .white
        tabBar.layer.borderWidth = 0.5
        tabBar.layer.borderColor = UIColor.withHex("#dddddd").cgColor
        tabBar.barStyle = .blackOpaque
        tabBarController?.tabBar.isTranslucent = false
    }

到这时候我们运行项目就能发现已经成功了。demo地址稍后附上!!!!!

上一篇下一篇

猜你喜欢

热点阅读