我正在开发 iOS 14 小部件扩展.
I'm developing a iOS 14 Widget extension.
我有 3 张图片要循环显示并创建一个时间线来显示这些图片.
I have 3 images to show in loop and creating a Timeline to show those images.
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
var entries: [ImageEntry] = []
let currentDate = Date()
for i in 1 ..< 4 {
let imageNumber = String(i)
let currentImage = "image_" + imageNumber
let entry = ImageEntry(date: currentDate,image: currentImage)
entries.append(entry)
}
let nextUpdateDate = Calendar.current.date(byAdding: .minute, value: 5, to: currentDate)!
let timeline = Timeline(entries: entries, policy: .after(nextUpdateDate))
completion(timeline)
}
struct WidgetModuleEntryView : View {
var entry: Provider.Entry
var body: some View {
Image(entry.image)
.resizable()
.scaledToFill()
}
}
始终呈现第一张图像,但不呈现后续图像.
First image always rendering but subsequent images are not.
可能是什么问题?
构建时间线需要为每个条目指定明确的条目日期.政策参数用于重新创建下一个时间线.
Constucting timeline you need to specify explicit entry date for each entry. The policy parameter is for recreating next timeline.
所以你的 getTimeline
应该看起来像
So your getTimeline
should look like
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
var entries: [ImageEntry] = []
let currentDate = Date()
for i in 1 ..< 4 {
let imageNumber = String(i)
let currentImage = "image_" + imageNumber
let entryDate = Calendar.current.date(byAdding: .minute, value: (i-1)*5, to: currentDate)!
let entry = ImageEntry(date: entryDate, image: currentImage)
entries.append(entry)
}
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
}
这篇关于在 iOS 14 小部件上渲染图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!