ios - 动画 TabBar 颜色变化

标签 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
}

谢谢!

最佳答案

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

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

关于ios - 动画 TabBar 颜色变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38848788/

相关文章:

ios - TableView 中的不同长度单元格在滚动 Swift 时使 View 对齐

swift - 所有设备都有共同的值(value)观?

ios - 从另一个类向当前 View Controller 添加 View

iphone - UIActivityIndi​​catorView 启动和停止

ios - Swift - 如何检查 TableView 是否为空

iphone - 从 iOS 设备读取蓝牙 MAC 地址

iphone - appdelegate 如何调用 viewcontroller 的方法?

iphone - 如何在 iOS 应用程序之间共享钥匙串(keychain)数据

ios - iOS 8 中没有出现 UITableViewCell 约束。我该如何缓解这种情况?

ios - 连接到外部数据库 (MySQL) - 使用 Swift