Swift 4.0.2 按下tab bar item时, item会有内缩的animation效果(如同Twitter的tab bar 效果一样)

时间:2023-03-09 08:41:34
Swift 4.0.2 按下tab bar item时, item会有内缩的animation效果(如同Twitter的tab bar 效果一样)

先上效果图:

Swift 4.0.2 按下tab bar item时, item会有内缩的animation效果(如同Twitter的tab bar 效果一样)

假设 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
}
}
}

效果图:

Swift 4.0.2 按下tab bar item时, item会有内缩的animation效果(如同Twitter的tab bar 效果一样)