Wednesday, December 27, 2023
HomeiOS Developmentios - What's the rationalization of observing a relentless knowledge change in...

ios – What’s the rationalization of observing a relentless knowledge change in SwiftUI View


After I examine the SwiftUI Apprentice ebook of Kodeco, I come throughout the under code snippet.

struct CountdownView: View {
  let date: Date
  @Binding var timeRemaining: Int
  let dimension: Double

  var physique: some View {
    Textual content("(timeRemaining)")  // 5
      .font(.system(dimension: dimension, design: .rounded))
      .padding()
      .onChange(of: date) { _ in  // 6
        timeRemaining -= 1
      }
  }
}

struct TimerView: View {
  @State personal var timeRemaining: Int = 3  // 1
  @Binding var timerDone: Bool  // 2
  let dimension: Double

  var physique: some View {
    TimelineView(  // 3
      .animation(
        minimumInterval: 1.0,
        paused: timeRemaining <= 0)) { context in
          CountdownView(  // 4
            date: context.date,
            timeRemaining: $timeRemaining,
            dimension: dimension)
        }
        .onChange(of: timeRemaining) { _ in
          if timeRemaining < 1 {
            timerDone = true  // 7
          }
        }
  }
}

struct TimerView_Previews: PreviewProvider {
  static var previews: some View {
    TimerView(timerDone: .fixed(false), dimension: 90)
  }
}

After I look at the code snippet, I firstly anticipated to be recreated CountdownView. However there can be no change on date property in that case. Then, I added onAppear modifier to CountdownView to detect the recreating of the view. In consequence, it’s created solely as soon as. I provide you with a query to assist me to grasp the view rending mechanism of SwiftUI, on which I nonetheless work that though date property is a continuing and doesn’t use @Binding how is it potential to look at the change of an information that’s declared as a relentless in SwiftUI?



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments