美文网首页
iOS 14-WidgetKit-桌面小组件-管理多套组件

iOS 14-WidgetKit-桌面小组件-管理多套组件

作者: CocoaJason | 来源:发表于2020-09-26 23:17 被阅读0次

假如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()
    }
}

  1. 组件一 (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.")
    }
}

相关文章

网友评论

      本文标题:iOS 14-WidgetKit-桌面小组件-管理多套组件

      本文链接:https://www.haomeiwen.com/subject/heahuktx.html