Hiding / showing views via ZStack in a Live Activity not working

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))

Replies

This is not supported. Networking, animations (outside of a very limited set of built-in transitions) and Timers do not work in Live Activities.

thanks @gcledat, there is no use of a timer here per se. Similar to the way that we can program a pause time using Text(timerInterval: dateRange, pauseTime: Date().addingTimeInterval(10), I would have thought we could also set an instruction for a specific date by using Task.sleep

If that's not possible, it's very sad indeed. It would be nice to trigger a change in a Live Activity, especially knowing full well in advance a change that needs to happen.

Probably what should happen, is Apple should provide is with the Timeline paradigm - the same that is used for regular Widgets