如何使用数组中的下一个 GIF 刷新视图

How to refresh view with the next GIF in the array

点击按钮后,我想用数组中的下一个 GIF 刷新当前显示的 GIF。我正在使用 SDWebImageSwiftUI 来显示 GIF。当我尝试更改数组值时显示以下错误消息:

Cannot invoke 'init' with an argument list of type '(url: URL?, options: SDWebImageOptions)'

import SwiftUI
import SDWebImage
import SDWebImageSwiftUI

struct TrainingView : View {
    @State private var forwardButtonTapped = false
    @Published var currentIndex = 0
    var GIFS = ["https://media3.giphy.com/media/xULW8v7LtZrgcaGvC0/giphy.gif?cid=790b7611cabeafc835b67239aa3fa3f3abc3df4322c3e4ea&rid=giphy.gif", "https://media1.giphy.com/media/6tHy8UAbv3zgs/giphy.gif?cid=790b7611cabeafc835b67239aa3fa3f3abc3df4322c3e4ea&rid=giphy.gif"]

    var body: some View {
            VStack(){

            AnimatedImage(url: URL(string: GIFS[currentIndex]), options: [.progressiveLoad])
                .scaledToFit()

            Button(action:{
                self.forwardButtonTapped.toggle()
            }) {Image("forwardButton")}

            if forwardButtonTapped {
                self.currentIndex += 1
            }
        }
    }
}

我认为使用 @Published 会在 currentIndex

上更改值后使用新 GIF 刷新视图

据我所知,在 HERE 中尝试示例时,在 SwiftUI 的主体中,我们只能渲染 UI,不能编写像 self.currentIndex += 1 这样的逻辑代码。所以当你在这里编写逻辑代码时,会出现一些奇怪的错误。只需将此代码放入按钮操作即可。我尝试了您的代码,在某处进行了编辑,它成功了。

struct TrainingView : View {
    @State private var forwardButtonTapped = false
    @State var currentIndex = 0
    var GIFS = ["https://media3.giphy.com/media/xULW8v7LtZrgcaGvC0/giphy.gif?cid=790b7611cabeafc835b67239aa3fa3f3abc3df4322c3e4ea&rid=giphy.gif", "https://media1.giphy.com/media/6tHy8UAbv3zgs/giphy.gif?cid=790b7611cabeafc835b67239aa3fa3f3abc3df4322c3e4ea&rid=giphy.gif"]

    var body: some View {
            VStack(){

            AnimatedImage(url: URL(string: GIFS[currentIndex]), options: [.progressiveLoad])
                .scaledToFit()

            Button(action:{
                self.currentIndex += 1
            }) {Text("CLICK @@@@@")}.frame(width: 150, height: 50, alignment: .leading)
        }
    }
}