先上效果图:
假设 tab bar items 有5个。tag为0,1,2,3,4。storyboard中tab bar controller继承的class叫做xxxVC。
class xxxVC: UITabBarController {
var storedImageViewArr:[UIImageView?] = []
private var times = [Int].init(repeating: 0, count: 5)
private var tempTimes = [Int].init(repeating: 0, count: 5)
let bounceAnimation = CAKeyframeAnimation(keyPath: "transform.scale")
override func viewDidLoad() {
super.viewDidLoad()
// create image views
getImageView()
//set path
setPath()
}//xxxVC class over line
//custom functions
extension xxxVC{
fileprivate func getImageView(){
times[0] = 1
storedImageViewArr = [0,1,2,3,4].map{ (offset) -> UIImageView in
let tempImageView = self.tabBar.subviews[offset].subviews.first as! UIImageView
tempImageView.contentMode = .center
return tempImageView
}
fileprivate func setPath(){
bounceAnimation.values = [1.0 ,0.6, 0.9, 1.15, 0.95, 1.02, 1.0]
bounceAnimation.duration = TimeInterval(0.5)
bounceAnimation.calculationMode = kCAAnimationCubic
}
}
//UITabBarControllerDelegate
extension xxxVC{
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if times[item.tag] == 0{
setAnimation(at: item.tag)
times = tempTimes
times[item.tag] += 1;return
}
}
}
效果图: