ios - 在SwiftUI中点击watchOS应用程序时如何更改图像
问题描述
点击时如何更改图像?
这是我的第一个 iOS 应用,更不用说手表应用了。我正在尝试构建一个简单的应用程序,当我点击图像时,它应该更改为下一个图像。到目前为止,我已经这样做了,并且正在显示第一张图像(test1),但是当我点击它时,什么也没有发生。
import SwiftUI
struct ContentView: View {
var body: some View {
var imgIndex: Int = 1
Image("test\(imgIndex)")
.resizable(resizingMode: .stretch)
.onTapGesture {
imgIndex = 2
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
仅供参考,我正在为 Apple Watch 构建一个应用程序。
解决方案
您需要使用@State:
struct ContentView: View {
@State private var imgIndex: Int = 1
var body: some View {
Image("test\(imgIndex)")
.resizable(resizingMode: .stretch)
.onTapGesture {
imgIndex = 2
}
}
}
推荐阅读
- google-chrome-extension - chrome扩展和pwa之间如何通信?
- c - 将指针传递给递归函数,该函数最初从 main - cast 传递给来自不同大小的整数的指针——C
- spring - 在编译 Spring mvc 示例时,在 pom.xml 中获取“无法调用“java.net.URI.getScheme()”,因为“uri”为空”
- python-3.x - 获取用户输入并在 Turtle Screen 上显示
- java - Jacoco 无法使用
在 pom.xml 中 - nginx - 如何优化我的 nginx 配置
- python - 如何从我的 txt 文件中搜索和显示所有记录?
- java - 如何在 java 代码中将 org.bouncycastle.rsa.allow_multi_use 设置为 true
- python - 我需要帮助为我的二维 CNN 模型找到正确的输入形状
- c# - 我在 Parallel.ForEach 循环中收到 TaskCanceledException,如何解决?