0

I am trying to have the images on my tab bar do a bounce animation when they are selected by the user, but so far I have only been able to get them to bounce if the user is on the current tab and then they re-select the tab. Is there a way to make them bounce whenever the user taps them and from any tab?

Here is my code:

    private var bounceAnimation: CAKeyframeAnimation = {
        let bounceAnimation = CAKeyframeAnimation(keyPath: "transform.scale")
        bounceAnimation.values = [1.0, 1.4, 0.9, 1.02, 1.0]
        bounceAnimation.duration = TimeInterval(0.4)
        bounceAnimation.calculationMode = CAAnimationCalculationMode.linear
        return bounceAnimation
    }()
    
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        let idx = tabBar.items?.firstIndex(of: item)
        let imageView: UIImageView? = self.tabBar.tabBarButtons[idx!].tabBarSwappableImageViews.first as? UIImageView
        imageView.layer.add(bounceAnimation, forKey: nil)
    }

Any help would be greatly appreciated. Thanks

A5omic
  • 65
  • 8

0 Answers0