0%

iOS new edge widget

最开始做的today widget(还是OC写的),后面发展到 home widget,然后 lock screen widget,是时候更新了。

Widget

Home Widget

两种可配置的 widget:

  1. StaticConfiguration,每个用户都是一样的展现;
  2. IntentConfiguration,可根据设定展示不同的样子;

widget 更新数据,使用 TimelineProvider:

struct ExampleTimelineEntry: TimelineEntry {
let date: Date
let title: String
}
struct ExampleTimelineProvider: TimelineProvider {
typealias Entry = ExampleTimelineEntry

// Provides a timeline entry representing a placeholder version of the widget.
// 初次占位,
func placeholder(in context: Context) -> ExampleTimelineEntry {
}

// Provides a timeline entry that represents the current time and state of a widget.
// 更新前触发,可以填充些假数据,觉得可以跟placeholder互用?
func getSnapshot(in context: Context, completion: @escaping (ExampleTimelineEntry) -> Void) {
}

// Provides an array of timeline entries for the current time and, optionally, any future times to update a widget.
// 真的数据返回时候刷新
func getTimeline(in context: Context, completion: @escaping (Timeline<ExampleTimelineEntry>) -> Void) {
}
}

Lock Screen Widget

貌似跟小组件没多大区别,就是增加了特别的模式。

样式,新增加了 WdigetFamily,其中桌面用到的是:

  • WidgetFamily.accessoryRectangular,圆角矩形,两个占位(一排四个占位)
  • WidgetFamily.accessoryCircular,圆形,一个占位

颜色,iOS只能使用 WidgetKit 里的:

  • vibrant``(iOS)
  • fullColor``(WatchOS)
  • accented``(WatchOS)

参考

iOS16锁屏小组件开发(WWDC2022-10050)
iOS16锁屏小组件
Building widgets for iOS applications with WidgetKit and SwiftUI
Creating a Widget Extension