Wednesday, October 28, 2026
HomeiOS Developmentios - Can't discover the bug in my code (SwiftUI-based animal-flash card...

ios – Can’t discover the bug in my code (SwiftUI-based animal-flash card app for teenagers)

[ad_1]

Beneath is a snippet from an academic animals app for teenagers that’s at present in beta testing. The under represents playing cards in a tab view that customers can swipe, containing a picture and the identify of the animal. There’s a characteristic inside the app that permits customers to show off the names proven by default for every animal, the place customers are required to faucet a button (Click on right here to disclose its identify) to disclose the displayed animal’s identify.

The issue I’m experiencing is that when customers swipe to the top of the array (i.e., the final animal card) AND when the present names characteristic is off by default, it’s transferring the identify of the penultimate card to the final (i.e., the second to final card’s identify is being proven for the final card, however corrects itself when the identify of the final card is tapped).

I’ve included screenshots to contextualise this a bit higher. I merely can not see why that is occurring. I’m comparatively new to coding so will respect any assist on this matter. Blissful to make clear any bits I’ve not defined or refine my query if wanted.Image1 Image2 Image3 Image4

Simply to reiterate, the playing cards show the names completely till it’s the remaining card (see photographs), the place the identify displayed is that of the previous animal.

GeometryReader { geo in
                TabView {
                    
                    ForEach (0..<mannequin.animals.depend, id: .self) { index in
                        
                        // Situation to test chosen index to match class chosen
                        if selectedIndex == 0 {
                            
                            if mannequin.animals[index].class == "farm" {
                                
                                ZStack (alignment: .topLeading) {
                                    ZStack {
                                        Rectangle()
                                            .foregroundColor(.white)
                                        
                                        VStack (spacing: 5) {
                                            
                                            ImageContainer(picture: mannequin.animals[index].picture, geo: geo)
                                            Button {
                                                
                                                revealName = mannequin.animals[index].identify
                                                
                                                if mannequin.animals[index].nameSound != "" {
                                                    
                                                    if mannequin.nameSoundIsOn != false {
                                                        
                                                        playSound(sound: mannequin.animals[index].nameSound, kind: "mp3")
                                                        
                                                    }
                                                    
                                                }
                                                
                                            } label: {
                                                
                                                if mannequin.isRevealed == false {
                                                    HiddenNameText(revealName: revealName)
                                                }
                                                else {
                                                    NameText(identify: mannequin.animals[index].identify)
                                                }
                                            }
                                            .buttonStyle(.plain)
                                           
                                        }
                                        // Faucet card to play animal sound characteristic
                                        .onTapGesture {
                                            if mannequin.animals[index].sound != "" {
                                                if mannequin.soundIsOn != false {
                                                    playSound(sound: mannequin.animals[index].sound, kind: "mp3")
                                                }
                                            }
                                        }
                                        // Use of onAppear to cease enjoying sounds as playing cards are swiped
                                        .onAppear {
                                            revealName = "Click on right here to disclose its identify"
                                            audioPlayer?.cease()
                                            /* playSound(sound: "swipe", kind: "mp3") earlier code for swipe card sound */
                                        }
                                    }
                                    .body(width: geo.measurement.width * 0.85, top: geo.measurement.top * 0.95, alignment: .middle)
                                    .clipped()
                                    .cornerRadius(10)
                                    .shadow(colour: .black, radius: 10, x: -5, y: 5)
                                    .tag(index)
                                    
                                    
                                    SpeakerView(soundIsOn: mannequin.soundIsOn)
                                    
                                }
                                
                            }
                            
                        }
                    }
                }
                .tabViewStyle(PageTabViewStyle(indexDisplayMode: .by no means))
                .indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .by no means))
            }

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments