模仿淘宝的tabbar的弹性动画效果
1.在tabbarViewController中重写
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
print(self.tabBar.subviews)
//MARK:遍历出UITabBarButton
for view:UIView in self.tabBar.subviews{
if view.isKind(of: NSClassFromString("UITabBarButton")!){
//MARK:转换view类型为UIControl
let tabbarButton:UIControl=view as! UIControl
tabbarButton.addTarget(self, action: #selector(tablebarButtonClick(tabbarButton:)), for: UIControlEvents.touchUpInside)
}
}
}
2.给tabbarButton添加弹性动画方法
//MARK:给tabbarButton添加弹性动画
@objc func tablebarButtonClick(tabbarButton:UIControl){
for imageView:UIView in tabbarButton.subviews{
if imageView.isKind(of: NSClassFromString("UITabBarSwappableImageView")!){
let animation:CAKeyframeAnimation = CAKeyframeAnimation.init()
animation.keyPath = "transform.scale"
animation.values = [1,1.3,0.9,1.15,0.9,1.5,1.03,1]
animation.duration = 1
animation.calculationMode = kCAAnimationCubic
//把动画添加上去就OK了
imageView.layer.add(animation, forKey: nil)
}
}
}