Swiftui 如何使用阵列中的下一个GIF刷新视图
点击按钮后,我想用阵列中的下一个GIF刷新当前显示的GIF。我用它来显示GIF。尝试更改数组值时显示以下错误消息: 无法使用“url:url?”类型的参数列表调用“init”?, 选项:SDWebImageOptions'Swiftui 如何使用阵列中的下一个GIF刷新视图,swiftui,sdwebimage,Swiftui,Sdwebimage,点击按钮后,我想用阵列中的下一个GIF刷新当前显示的GIF。我用它来显示GIF。尝试更改数组值时显示以下错误消息: 无法使用“url:url?”类型的参数列表调用“init”?, 选项:SDWebImageOptions' 我认为一旦currentIndex上的值发生更改,使用@Published将用新的GIF刷新视图,正如我在SwiftUI主体中尝试示例时所知,在SwiftUI中,我们只能呈现UI,不能编写self.currentIndex+=1这样的逻辑代码。所以当你在这里写逻辑代码时,会出
我认为一旦currentIndex上的值发生更改,使用@Published将用新的GIF刷新视图,正如我在SwiftUI主体中尝试示例时所知,在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)
}
}
}
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)
}
}
}