假如APP需要不同的功能的多种组件可查看此文章
关键代码
@main
struct Widgets: WidgetBundle {
@WidgetBundleBuilder
var body: some Widget {
Widget_Extension()
Extension_Two()
}
}
1.主入口
Main_Extension.swift
import SwiftUI
@main
struct Widgets: WidgetBundle {
@WidgetBundleBuilder
var body: some Widget {
Widget_Extension()
Extension_Two()
}
}
- 组件一 (Widget_Extension() )
Widget_Extension.swift
import WidgetKit
import SwiftUI
import Intents
struct Provider: IntentTimelineProvider {
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), configuration: ConfigurationIntent())
}
func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) {
let entry = SimpleEntry(date: Date(), configuration: configuration)
completion(entry)
}
func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
var entries: [SimpleEntry] = []
// Generate a timeline consisting of five entries an hour apart, starting from the current date.
let currentDate = Date()
for hourOffset in 0 ..< 5 {
let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
let entry = SimpleEntry(date: entryDate, configuration: configuration)
entries.append(entry)
}
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
}
}
struct SimpleEntry: TimelineEntry {
let date: Date
let configuration: ConfigurationIntent
}
struct Widget_ExtensionEntryView : View {
var entry: Provider.Entry
var body: some View {
Text(entry.date, style: .time)
}
}
struct Widget_Extension: Widget {
let kind: String = "Widget_Extension"
var body: some WidgetConfiguration {
IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in
Widget_ExtensionEntryView(entry: entry)
}
.configurationDisplayName("My Widget")
.description("This is an example widget.")
}
}
struct Widget_Extension_Previews: PreviewProvider {
static var previews: some View {
Widget_ExtensionEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent()))
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
}
3.组件二(Extension_Two())
Extension_Two.swift
import WidgetKit
import SwiftUI
import Intents
struct Extension_Two_Provider: IntentTimelineProvider {
func placeholder(in context: Context) -> Extension_Two_Entry {
Extension_Two_Entry(date: Date())
}
func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Extension_Two_Entry) -> ()) {
let entry = Extension_Two_Entry(date: Date())
completion(entry)
}
func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
var entries: [Extension_Two_Entry] = []
// Generate a timeline consisting of five entries an hour apart, starting from the current date.
let currentDate = Date()
for hourOffset in 0 ..< 5 {
let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
let entry = Extension_Two_Entry(date: entryDate)
entries.append(entry)
}
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
}
}
struct Extension_Two_Entry: TimelineEntry {
let date: Date
}
struct Extension_TwoEntryView : View {
var entry: Extension_Two_Provider.Entry
var body: some View {
Text(entry.date, style: .time)
}
}
struct Extension_Two: Widget {
let kind: String = "Extension_Two"
var body: some WidgetConfiguration {
IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Extension_Two_Provider()) { entry in
Extension_TwoEntryView(entry: entry)
}
.configurationDisplayName("My Widget Extension_Two")
.description("Extension_Two.")
}
}
网友评论