Created
December 14, 2018 13:54
-
-
Save Denismih/77ee6ac8c78a3bd3c7dcf8016bd7b952 to your computer and use it in GitHub Desktop.
AnimatedTabBarController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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