0
struct AnimationView: UIViewRepresentable {
    @Binding var cnt: Int
    var imageView: UIImageView = UIImageView(image: effctPicker(cnt: 0))
    func makeUIView(context: Self.Context) -> UIImageView {
        return imageView
    }
    func updateUIView(_ uiView: UIImageView, context: UIViewRepresentableContext<AnimationView>) {
        if self.cnt == 0{
            self.imageView.image = effctPicker(cnt: 0)
        }
        else {
            self.imageView.image = effctPicker(cnt: self.cnt)
        }
    }
}

struct ContentView: View {
    @State var count:Int = 0
    @State var audioPlayer: AVAudioPlayer!
    @State var bg: String = "bg"

    var body: some View {
                Button(action:{
                    count += 1
                    switch count
                    {
                    case 1:
                        self.bg = "bg_2"
                    default:
                        count = 0
                        self.bg = "bg"
                    }
                }, label: {
                    ZStack {
                    Image(bg)
                        .resizable()
                        AnimationView(cnt: $count)
                    }.onAppear {
                        let sound = Bundle.main.path(forResource: "freelove", ofType: "mp3")
                        self.audioPlayer = try! AVAudioPlayer(contentsOf: URL(fileURLWithPath: sound!))
                        self.audioPlayer.play()
                    }
                }
                )
    
            }
}
var springImages = [UIImage(named: "eft1")!, UIImage(named: "eft2")!,UIImage(named: "eft3")!, UIImage(named: "eft4")!]
var rainImages = [UIImage(named: "raineft_1")!, UIImage(named: "raineft_2")!,UIImage(named: "raineft_3")!]


func effctPicker(cnt : Int) ->UIImage{
    if cnt == 0{
        return UIImage.animatedImage(with: springImages, duration: 0.4)!
    }
    else {
        return UIImage.animatedImage(with: rainImages, duration: 0.4)!
    }
    
}

First of all, I'm sorry that my english skill is not good. And this is my first question in stackoverflow

This is my animation view I want when i press the button, binded "cnt" will change and updaateUIView will be called. But it didn't worked. Should I add coordinator? If so, what should I do?

jin96
  • 1
  • 1
  • 1
    You say "when I press the button", but your code doesn't show a button. Can you include enough for a [mre]? – jnpdx Jun 19 '21 at 06:34
  • This is brief code Button(action: { count ++ }) { AnimationView($count) } – jin96 Jun 19 '21 at 06:40
  • 1
    Try to change uiView.image instead of self.imageView in update view method – Raja Kishan Jun 19 '21 at 06:41
  • Is that *really* the code for your `Button`? Looks like you're trying to shorten things... Can you edit your question with the *real* code? Can you include `effctPicker`, too? – jnpdx Jun 19 '21 at 06:44
  • I agree with @RajaKishan. Storying a reference to a `UIImageView` in a transient `struct` doesn't seem reliable. Mutate the `uiView` passed in and don't try to store a reference to it. – jnpdx Jun 19 '21 at 06:56
  • I’ll try that when i’m back home… thank you all! – jin96 Jun 19 '21 at 07:00
  • 2
    If condition is pointless. No need condition – Raja Kishan Jun 19 '21 at 07:03
  • oh i tried uiView.image and it finally worked! thank you! – jin96 Jun 19 '21 at 14:06

0 Answers0