// 去掉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
}
最终效果如下:
注:相关教程知识阅读请移步到swift教程频道。









