适用于 SwiftUI 的节能 GIF 显示屏

吉菲

Giffy 是一个轻量级的包,允许您在 SwiftUI 视图中显示动画 GIF。由经过现场测试的FLAnimatedImage提供支持,所有显示的动画都将非常高效,同时提高内存效率。

Giffy 可用于显示本地存储和远程 URL 的 GIF 文件,提供了一个非常熟悉的 API,在 SwiftUI 环境中感觉宾至如归。

要求

  • iOS 14.0 或更高版本

用法

本地文件

要显示本地 GIF 文件中的动画,请使用以下组件:Giffy

Giffy("cat")

格拉瓦桑德特拉 2023-05-01 às 12 36 15

If your GIF is stored on another bundle outside your project’s main bundle, you can specify the Bundle to load the animation from.

// Loads an animation from the provided bundle
LottieView("cat", bundle: DesignSystem.Bundle.main)

Or if you want to use the path to your GIF file, you can simply pass it as well:

// Loads a GIF file from the provided path
LottieView(path: URL(string: "path/to/cat.gif")!)

Remote Files

If you want to display a GIF from the web, use the component. You can also setup a placeholder view to be displayed while the view is beign loaded and a view to be displayed in place in case the GIF fails to load:AsyncGiffy

let url = URL(string: "https://media.giphy.com/media/RrVzUOXldFe8M/giphy.gif")!

AsyncGiffy(url: url) { phase in
    switch phase {
    case .loading:
        ProgressView()
    case .error:
        Text("Failed to load GIF")
    case .success(let giffy):
        giffy
    }
}

格拉瓦桑德特拉 2023-05-01 às 12 45 16

Features

Apart from displaying GIFs, you can also set an action to be executed each time an animation loops with the modifier:.onLoop(:)

@State let jumpCount = 0
VStack {
    AsyncGiffy(url: gifURL) { phase in
        switch phase {
        case .loading:
            ProgressView()
        case .error:
            Text("Failed to load GIF")
        case let .success(giffy):
            giffy
                .onLoop {
                    jumpCount += 2
                }
        }
    }
    Text("Jump count: \(jumpCount)")
}

Gravação de Tela 2023-05-01 às 12 51 21

Installation

Currently, Giffy can be installed through Swift Package Manager

To add this package to your project, go to in Xcode’s menu and paste this Github page URL on the search barFile -> Swift Package Manager -> Add Package Dependency...

You can also add Giffy by adding it as a dependecy on your Package.swift file:

dependencies: [
    .package(url: "https://github.com/tfmart/Giffy.git", .upToNextMajor(from: "1.0.0"))
],
targets: [
    .target(name: "MyProject", dependencies: ["Giffy""])
]

GitHub

点击跳转