转载请注明出处:http://www.jianshu.com/p/d487a87105e7 ,本文出自老初的简书.
如何自定义TabBarController我们在上一篇已经讲过,现在为自定义的TabBar增加动画效果。
直接上代码:
// 用来记录当前选中按钮
private var currentSelectedButton = UIButton()
// 用来指示选中的背景
private var selectionIndicatorImageView: UIImageView!
// 单个item的宽度
private var itemWidth: CGFloat!
在viewDidLoad()方法中加入以下代码:
override func viewDidLoad() {
super.viewDidLoad()
// 记录TabBarController自带TabBar的位置
let rect = self.tabBar.frame
// 移除TabBarController自带的TabBar
self.tabBar.removeFromSuperview()
// 自定义TabBar的背景
let backgroundView = UIView(frame: rect)
backgroundView.backgroundColor = UIColor(patternImage: UIImage(named: "TabBarBG")!)
self.view.addSubview(backgroundView)
itemWidth = backgroundView.frame.size.width / CGFloat(self.viewControllers!.count)
selectionIndicatorImageView = UIImageView(frame: CGRectMake(0, 0, itemWidth, backgroundView.frame.size.height))
selectionIndicatorImageView.image = UIImage(named: "TabBarBGSel")
backgroundView.addSubview(selectionIndicatorImageView)
for var i = 0; i < viewControllers!.count; i++ {
let button = CGMTabBarButton(frame: CGRectMake(itemWidth * CGFloat(i), 0, itemWidth, backgroundView.frame.size.height))
let image = UIImage(named: "TabBar\(i)")!
let selImage = UIImage(named: "TabBar\(i)Sel")!
button.setImage(image, forState: UIControlState.Normal)
button.setImage(selImage, forState: UIControlState.Selected)
button.addTarget(self, action: "onClick:", forControlEvents: UIControlEvents.TouchUpInside)
button.tag = i
// 去掉buttond的高光效果
button.adjustsImageWhenHighlighted = false
backgroundView.addSubview(button)
}
}
添加onClick方法,动画效果也在这里面实现:
func onClick(button: UIButton) {
// 将上个选中俺就设置为为选中
self.currentSelectedButton.selected = false
// 当前按钮设置为选中
button.selected = true
self.currentSelectedButton = button
let x = CGFloat(button.tag) + 0.5
// 为TabBarItem的背景添加动画
UIView.animateWithDuration(0.4, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 10.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
self.selectionIndicatorImageView.center.x = self.itemWidth * x
}, completion: nil)
self.selectedIndex = button.tag
}
最终效果如下:
GIF截的不好,各位老师都是用的什么截图软件啊?