UITabBar项下添加gif动画
Add gif animation under UITabBar item
我有一个 UITabBarController
有 5 个标签。
我有一个 GIF
文件要放在每个选项卡下。
我正在使用 3rd party API (UIImage
extension) called SwiftGif
来使用 gif,所以问题也可以这样问:
"How can I place a UIImage
as the background of my UITabBar
"
但我想说得更具体一些,以帮助您更好地理解。
试试这个来改变你的 tabBar 的背景 self.tabBarController?.tabBar.backgroundImage = UIImageView(image: UIImage.gifWithName("yourGifName"))
请记住,这个语法来自 Swift 3 - 如果你正在使用 Swift 2,你可能必须稍微改变一下
Swift 4 :
一个 class 为你的标签栏,然后像这样设置图像:
import UIKit
import SwiftGifOrigin ///to install that pod: https://github.com/bahlo/SwiftGifCreate
class TabbarVC: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let myTabBarItem1 = (self.tabBar.items?[0])! as UITabBarItem
myTabBarItem1.image = UIImage(named: "your unselected image")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
myTabBarItem1.selectedImage = UIImage.gif(asset: "your gif")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
myTabBarItem1.title = ""
myTabBarItem1.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
}
}
这是对我有用的解决方案,只需在 Tabbar 中为所选图像设置 gif class。
import UIKit
class TabbarVC: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
//Set Selected image for all items, I'm doing only for item 1
let myTabBarItem1 = (self.tabBar.items?[0])! as UITabBarItem
myTabBarItem1.selectedImage = UIImage.gifImageWithName("image name")
}
}
这是从 URL/Bundle/Name
.
加载 Gif
的扩展
extension UIImage {
public class func gifImageWithData(_ data: Data) -> UIImage? {
guard let source = CGImageSourceCreateWithData(data as CFData, nil) else {
print("image doesn't exist")
return nil
}
return UIImage.animatedImageWithSource(source)
}
public class func gifImageWithURL(_ gifUrl:String) -> UIImage? {
guard let bundleURL:URL = URL(string: gifUrl)
else {
print("image named \"\(gifUrl)\" doesn't exist")
return nil
}
guard let imageData = try? Data(contentsOf: bundleURL) else {
print("image named \"\(gifUrl)\" into NSData")
return nil
}
return gifImageWithData(imageData)
}
public class func gifImageWithName(_ name: String) -> UIImage? {
guard let bundleURL = Bundle.main
.url(forResource: name, withExtension: "gif") else {
print("SwiftGif: This image named \"\(name)\" does not exist")
return nil
}
guard let imageData = try? Data(contentsOf: bundleURL) else {
print("SwiftGif: Cannot turn image named \"\(name)\" into NSData")
return nil
}
return gifImageWithData(imageData)
}
我有一个 UITabBarController
有 5 个标签。
我有一个 GIF
文件要放在每个选项卡下。
我正在使用 3rd party API (UIImage
extension) called SwiftGif
来使用 gif,所以问题也可以这样问:
"How can I place a UIImage
as the background of my UITabBar
"
但我想说得更具体一些,以帮助您更好地理解。
试试这个来改变你的 tabBar 的背景 self.tabBarController?.tabBar.backgroundImage = UIImageView(image: UIImage.gifWithName("yourGifName"))
请记住,这个语法来自 Swift 3 - 如果你正在使用 Swift 2,你可能必须稍微改变一下
Swift 4 : 一个 class 为你的标签栏,然后像这样设置图像:
import UIKit
import SwiftGifOrigin ///to install that pod: https://github.com/bahlo/SwiftGifCreate
class TabbarVC: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let myTabBarItem1 = (self.tabBar.items?[0])! as UITabBarItem
myTabBarItem1.image = UIImage(named: "your unselected image")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
myTabBarItem1.selectedImage = UIImage.gif(asset: "your gif")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
myTabBarItem1.title = ""
myTabBarItem1.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
}
}
这是对我有用的解决方案,只需在 Tabbar 中为所选图像设置 gif class。
import UIKit
class TabbarVC: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
//Set Selected image for all items, I'm doing only for item 1
let myTabBarItem1 = (self.tabBar.items?[0])! as UITabBarItem
myTabBarItem1.selectedImage = UIImage.gifImageWithName("image name")
}
}
这是从 URL/Bundle/Name
.
Gif
的扩展
extension UIImage {
public class func gifImageWithData(_ data: Data) -> UIImage? {
guard let source = CGImageSourceCreateWithData(data as CFData, nil) else {
print("image doesn't exist")
return nil
}
return UIImage.animatedImageWithSource(source)
}
public class func gifImageWithURL(_ gifUrl:String) -> UIImage? {
guard let bundleURL:URL = URL(string: gifUrl)
else {
print("image named \"\(gifUrl)\" doesn't exist")
return nil
}
guard let imageData = try? Data(contentsOf: bundleURL) else {
print("image named \"\(gifUrl)\" into NSData")
return nil
}
return gifImageWithData(imageData)
}
public class func gifImageWithName(_ name: String) -> UIImage? {
guard let bundleURL = Bundle.main
.url(forResource: name, withExtension: "gif") else {
print("SwiftGif: This image named \"\(name)\" does not exist")
return nil
}
guard let imageData = try? Data(contentsOf: bundleURL) else {
print("SwiftGif: Cannot turn image named \"\(name)\" into NSData")
return nil
}
return gifImageWithData(imageData)
}