Reputation: 363
I have imported a a confettiCannon code for my app, which needs a binding Int in the view to be executed.
https://github.com/simibac/ConfettiSwiftUI#-installation
struct MyView: View {
@ObservedObject var game: Game
@State private var counter: Int = 0
var body: some View {
HStack { }.frame(height: 140)
.onTapGesture { counter += 1 }
.confettiCannon(counter: $counter)
}
Instead of using the @State var, I would like to use the animation when a value in my game data changes (which is an ObservedObject):
.confettiCannon(counter: $game.counter)
But this leads to the following error:
Cannot convert value of type 'Int' to expected argument type 'Binding'
What needs to be adapted to make this connection work?
Upvotes: 0
Views: 279
Reputation: 36782
works well for me with the following example code:
import Foundation
import SwiftUI
import ConfettiSwiftUI
struct ContentView: View {
@StateObject var game = Game() // <-- here
var body: some View {
MyView(game: game) // <-- here
}
}
class Game: ObservableObject {
@Published var counter: Int = 0 // <-- here
}
struct MyView: View {
@ObservedObject var game: Game
var body: some View {
HStack {
Text("tap me for confetti")
}.frame(height: 140)
.onTapGesture { game.counter += 1 }
.confettiCannon(counter: $game.counter)
}
}
Upvotes: 0