动画TabBar颜色更改

时间:2016-08-09 10:53:54

标签: ios iphone swift cocoa-touch uitabbarcontroller

我有一个UITabBar,每个标签都会随时更改其颜色。

我希望它是动画的(在notSelectedColor到SelectedColor之间0.5秒),我该怎么做?

我正在用这样的颜色重新绘制图像:

func imageWithColor(color: UIColor) -> UIImage {
    UIGraphicsBeginImageContextWithOptions(size, false, scale)

    let context = UIGraphicsGetCurrentContext()
    CGContextTranslateCTM(context, 0.0, size.height)
    CGContextScaleCTM(context, 1.0, -1.0)
    CGContextSetBlendMode(context, CGBlendMode.Normal)

    let rect = CGRect(origin: CGPointZero, size: size)
    CGContextClipToMask(context, rect, CGImage)
    color.setFill()
    CGContextFillRect(context, rect)

    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    return newImage
}
谢谢你!

1 个答案:

答案 0 :(得分:2)

您可以通过以下代码为标签颜色设置动画:

let tabBar: UITabBar? = self.tabBarController?.tabBar
        UIView.transitionWithView(tabBar!, duration: 1.0, options: [.BeginFromCurrentState, .TransitionCrossDissolve], animations: {
            self.tabBarController?.tabBar.tintColor = UIColor.purpleColor()
            }, completion: nil)

我也为你做了一个示例项目。下载sample project here

enter image description here

相关问题