WidgetKit - Mudium 代码记录
2020-10-02 本文已影响0人
CocoaJason
//
// ssss.swift
// ssss
//
// Created by 黄震 on 2020/10/2.
//
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>) -> ()) {
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 ssssEntryView : View {
var entry: Provider.Entry
var body: some View {
GeometryReader { geo in
VStack(alignment: .leading) {
VStack(alignment: .leading) {
Text("标题1")
.padding(.top,16)
.font(Font.system(size: 16))
.background(Color.green)
Spacer()
Text("标题2")
.font(Font.system(size: 16))
.padding(.top,3)
.background(Color.green)
Spacer()
Text("内容2")
.font(Font.system(size: 16))
.padding(.top,3)
.background(Color.purple)
Spacer()
Text("标题3")
.font(Font.system(size: 16))
.padding(.top,1)
.background(Color.gray)
}
.frame(width: geo.size.width, height: geo.size.height - 45, alignment: .leading)
.padding(.leading,15)
.background(Color.red)
HStack(alignment: .top, spacing: 0, content: {
Text("222")
.background(Color.purple)
.padding(EdgeInsets(top: 0, leading: 15, bottom: 0, trailing: 0))
Spacer()
Text("555")
.background(Color.purple)
.padding(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 15))
})
.frame(width: geo.size.width, height: 45, alignment: .leading)
.background(Color.green)
Spacer()
}
.frame(width: geo.size.width, height: geo.size.height, alignment: .topLeading)
.background(Color.blue)
}
}
}
@main
struct ssss: Widget {
let kind: String = "ssss"
var body: some WidgetConfiguration {
IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in
ssssEntryView(entry: entry)
}
.configurationDisplayName("My Widget")
.description("This is an example widget.")
}
}
struct ssss_Previews: PreviewProvider {
static var previews: some View {
ssssEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent()))
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
}
WX20201002-123848@2x.png