Saturday, November 18, 2023
HomeiOS Developmentios - SwiftUI's .contentTransition(.numericText()) not working as anticipated when utilizing .onChange modifier?

ios – SwiftUI’s .contentTransition(.numericText()) not working as anticipated when utilizing .onChange modifier?


I am making an attempt to implement SwiftUI’s new .contentTransition(.numericText()) as described in this submit. However I am unable to work out why it really works on a button faucet, however I am unable to get it to work the place I would like, in an .onChange modifier? 🤔

struct ContentView: View {
    @State personal var newNumberForAnimation = 98.0
    @State personal var quantity: Double = 99.0
    
    var physique: some View {
        VStack {
        Textual content("(Int(quantity))")
            .font(.system(dimension: 36))
            .contentTransition(.numericText())
            Button {
                         //This works when uncommented
                         //withAnimation {
                             quantity = .random(in: 90 ..< 100)
                         //}
                     } label: {
                         Textual content("Random")
                     }

    }
        //Why does this not work? 
        .onChange(of: quantity, preliminary: true) { oldNumber, newNumber in
            withAnimation {
                newNumberForAnimation = newNumber
            }
        }
     }
}

#Preview {
    ContentView()
}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments