使用计时器切换图像(SwiftUI)

问题描述 投票:-1回答:1

我使用UIPageViewController显示图像。我需要一个计时器,每5秒钟显示一个接一个的图像。如何使用计时器在5秒后启动事件以移至下一张图像?

screenshot

ios swift swiftui uipageviewcontroller
1个回答
0
投票

使用Timer.publish创建计时器实例字段,如下所示:

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()
© www.soinside.com 2019 - 2024. All rights reserved.