Skip to content

Instantly share code, notes, and snippets.

@Denismih
Created December 14, 2018 13:54
Show Gist options
  • Save Denismih/77ee6ac8c78a3bd3c7dcf8016bd7b952 to your computer and use it in GitHub Desktop.
Save Denismih/77ee6ac8c78a3bd3c7dcf8016bd7b952 to your computer and use it in GitHub Desktop.
AnimatedTabBarController
class AnimatedTabBarController: UITabBarController {
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.3)
bounceAnimation.calculationMode = CAAnimationCalculationMode.cubic
return bounceAnimation
}()
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
// find index if the selected tab bar item, then find the corresponding view and get its image, the view position is offset by 1 because the first item is the background (at least in this case)
guard let idx = tabBar.items?.index(of: item), tabBar.subviews.count > idx + 1, let imageView = tabBar.subviews[idx + 1].subviews.flatMap { $0 as? UIImageView }.first else {
return
}
imageView.layer.add(bounceAnimation, forKey: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment