ios - 使用计时器切换图像 (SwiftUI)
解决方案
用于Timer.publish
创建计时器实例字段,如下所示:
let images = [...] // Array of image names to show
@State var activeImageIndex = 0 // Index of the currently displayed image
let imageSwitchTimer = Timer.publish(every: 5, on: .main, in: .common)
.autoconnect()
然后使用.onReceive()
任意视图的修饰符转到下一张图片:
Image(named: images[activeImageIndex])
.onReceive(imageSwitchTimer) { _ in
// Go to the next image. If this is the last image, go
// back to the image #0
self.activeImageIndex = (self.activeImageIndex + 1) % self.images.count
}
推荐阅读
- ruby-on-rails - 为什么十进制保持值像 0.219e3 而不是我想要的 219.0
- python - 将一个数字的所有更改为numpy数组中的另一个数字
- php - 编码问题特殊字符 csv 到 php
- c++ - ADL 在 constexpr 函数中不起作用(仅限 clang)
- r - ggplot2中同一分组箱线图上的多个变量*具有不同的选项*?
- c - 如何在运行它的程序仍在运行时清除文件数据的内存
- pandas - 熊猫 - 按出现顺序排列
- php - 指向外部样式表的链接在 php 中未按预期工作包括
- c++ - 如何从 LCP 数组和 Suffix 数组构造 Suffix 树
- python - 加载保存的 cookie 后页面未登录