話不多說,我們直接開始箫章。首先我們用cocopods安裝一下我們這個框架诊笤。cocopods的使用就不多說了,大家可以自行百度产舞。
然后我們創(chuàng)建一個controller
繼承我們RAMAnimatedTabBarController
魂奥。當然不要忘記引入RAMAnimatedTabBarController
不然可能會找不到。
接下來我們創(chuàng)建一個類繼承至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()
}
??是兩個加載的方法。我們現(xiàn)在只需要添加我們的childVC
就好了具體代碼
??的代碼里面我們可能要添加多個tabbar
并且給不同的tabbar
設置不同的動畫攘已,這時候我們只需要創(chuàng)建多個集成至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:"調(diào)度管理", 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
}
到這時候我們運行項目就能發(fā)現(xiàn)已經(jīng)成功了。demo地址稍后附上Q7涂薄!O靠簟剧防!