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

效果图:

posted @ 2017-12-15 15:09  0x5f375a86  阅读(427)  评论(0编辑  收藏  举报