我正在制作一个非常简单的 WidgetKit 复杂功能,不幸的是,每次表盘唤醒时它都会闪烁。我的 Apple Watch 已更新为 WatchOS 9.4。我以前经历过这种情况,但似乎通过卸载并重新安装 watch 应用程序来解决该问题。不幸的是,事实证明这不再成功。有人有什么想法吗?这是我的代码。您可以假设 getCount()
立即返回 0 - 将其替换为此值不会停止闪烁。
//
// DrinkCountComplications.swift
// DrinkCountComplications
//
// Created by Bryce Sandlund on 11/6/22.
//
import WidgetKit
import SwiftUI
struct Provider: TimelineProvider {
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), count: getCount())
}
func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {
let entry = SimpleEntry(date: Date(), count: getCount())
completion(entry)
}
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
completion(Timeline(entries: [SimpleEntry(date: Date(), count: getCount())], policy: TimelineReloadPolicy.never))
}
}
struct SimpleEntry: TimelineEntry {
let date: Date
let count: Int
}
struct DrinkCountComplicationsEntryView : View {
var entry: SimpleEntry
var body: some View {
let link = URL(string: "myApp://widgetClick")?
.appending(queryItems: [URLQueryItem(name: "count", value: String(entry.count))])
ZStack {
Text("DRINK")
.offset(x: 0, y: -15)
.foregroundColor(Color.orange)
.font(.system(size: 10, weight: .semibold))
Text(String(entry.count))
.font(.title)
.offset(x: 0, y: 0)
Text("COUNT")
.offset(x: 0, y: 15)
.foregroundColor(Color.orange)
.font(.system(size: 10, weight: .semibold))
}
.unredacted()
.widgetURL(link)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background()
}
}
@main
struct DrinkCountComplications: Widget {
let kind: String = "DrinkCountComplications"
var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider()) { entry in
DrinkCountComplicationsEntryView(entry: entry)
}
.configurationDisplayName("Drink Count Widget")
.description("Display drink count & add a drink")
}
}
struct DrinkCountComplications_Previews: PreviewProvider {
static var previews: some View {
DrinkCountComplicationsEntryView(entry: SimpleEntry(date: Date(), count: getCount()))
.previewContext(WidgetPreviewContext(family: .accessoryRectangular))
}
}
最佳答案
我终于能够在我的应用程序中隔离它,然后在示例项目中重现它。我不能保证这将是唯一可能的原因 - 特别是因为它毫无意义!
对我来说,原因是在我的 WidgetKit View
返回的 View 上使用 .widgetURL(_)
修饰符:
struct RedrawWidgetsEntryView : View {
var entry: Provider.Entry
var body: some View {
Capsule()
.fill(.white)
.frame(width: .infinity, height: .infinity)
.widgetURL(URL(string: "https://apple.com")!)
}
}
我尝试了多种方法,但出现了许多问题。我找到的解决方法似乎是有效的,即将 widgetURL 移动到屏幕外 View ,然后我将它变成了一个修饰符。
@available(watchOS 9, *)
fileprivate struct WidgetURLWatchWorkaround: ViewModifier {
let url: URL
func body(content: Content) -> some View {
content
.background {
Color.white
.frame(width: 1, height: 1)
.position(x: -1000, y: -1000)
.widgetURL(url)
}
}
}
@available(watchOS 9, *)
extension View {
func widgetURLWatch(_ url: URL) -> some View {
modifier(WidgetURLWatchWorkaround(url: url))
}
}
编辑 1:将 EmptyView()
替换为 Color.black
,因为虽然它在 sim 中工作,但在设备上我猜它(以及 Color.clear
) 得到优化。
编辑 2:由于前一个解决方案存在问题,使用较新的解决方案进行了更新。
关于ios - WatchOS 复杂功能闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76105694/