I have created the following views inside a playground. I want to set views within a ZStack
to appear and disappear over time.
I have debugged the attributes being passed over and have found everything seems to be perfectly fine there. Seems to be a matter of the View
itself.
This works fine in a playground, but will not display at all in a live activity.
Is there an issue doing this with Widgets or is it not supported?
import SwiftUI
import WidgetKit
import PlaygroundSupport
struct CyclesView: View {
let dateRange: ClosedRange<Date>
@State private var shouldShow = false
var body: some View {
viewBody()
.padding()
.opacity(shouldShow ? 1.0 : 0.0)
}
@ViewBuilder
private func viewBody() -> some View {
VStack(alignment: .trailing) {
Text(
timerInterval: dateRange)
}
.task {
await hideAfterDelay()
}
.task {
await showAfterDelay()
}
}
private func showAfterDelay() async {
try? await Task.sleep(for: .seconds(dateRange.lowerBound.timeIntervalSinceNow))
shouldShow = true
}
private func hideAfterDelay() async {
try? await Task.sleep(for: .seconds(dateRange.upperBound.timeIntervalSinceNow))
shouldShow = false
}
}
struct CyclesStack: View {
static let referenceDate = Date()
let timelines: [ClosedRange<Date>] = [
referenceDate...referenceDate.addingTimeInterval(10),
referenceDate.addingTimeInterval(10)...referenceDate.addingTimeInterval(20),
referenceDate.addingTimeInterval(20)...referenceDate.addingTimeInterval(30),
referenceDate.addingTimeInterval(30)...referenceDate.addingTimeInterval(40)
]
var body: some View {
ZStack {
ForEach(timelines, id: \.lowerBound) { range in
CyclesView(
dateRange: range
)
}
}
.padding()
}
}
PlaygroundPage.current.setLiveView(CyclesStack()
.frame(width: 200, height: 100))