HomeiOS Developmentios - How you can use take away() and allowhitTesting to take...

ios – How you can use take away() and allowhitTesting to take away the article from the view utilizing SwiftUI?


The difficulty I’ve discovered is that when the I faucet on the button “thumbs up”, the cardboard does not take away from the view. The preliminary goal of this view is to for person to check playing cards by tapping buttons and on the identical time “eradicating” them from the view and giving then the class quantity (1,2,3,4).
I am at present utilizing take away variable and withAnimation in order that the cardboard could be eliminated with animation, nevertheless, nothing occurs when the button is tapped.

This is the code for the view the place the stack of playing cards is positioned:

struct FlashcardSetView: View {
    
    let units: FlashSets
    @State personal var selectedCards: [FlashCardData] = [] // Preserve monitor of chosen playing cards
    @State personal var currentlySelectedCard: FlashCardData?
    @State var isLearned = false //1
    @State var isThink = false  //2
    @State var isHard = false   // 3
    @State var isRepeat = false // 4
    @State personal var currentCardIndex = 0
    var removing: (()-> Void)? = nil
    @State personal var playing cards: [FlashCardData] = []
    func removeCard(_ card: FlashCardData) {
        if let index = playing cards.firstIndex(of: card) {
            playing cards.take away(at: index)
        }
    }
    
    
    var physique: some View {
        NavigationView {
            ZStack {
                
                ForEach(playing cards, id: .self) { card in
                    
                    
                    SingleFlashCard(card: card,
                                    removing: {
                        withAnimation {
                            self.removeCard(card)
                            print("Card eliminated")
                        }
                        // Deal with card removing right here
                        
                    }, isLearned: $isLearned,
                                    isThink: $isThink,
                                    isHard: $isHard,
                                    isRepeat: $isRepeat)
                    .transition(.uneven(insertion: .transfer(edge: .backside), removing: .opacity))
                    
                    .gesture(
                        TapGesture()
                            .onEnded {
                                // Deal with button faucet right here
                                handleButtonTap(card: card)
                                print("Button tapped for card: (card)")
                            }
                    )
                    //.opacity(isLearned ? 500 : 0)
                    
                    .allowsHitTesting(true)
                    
                    .onAppear {
                        currentlySelectedCard = card
                        print("Card appeared: (card)")
                    }
                    
                    
                    
                }
                
                
            }
            
        }
        HStack {
            Button(motion: {
                // Deal with button motion
                //  isTapped.toggle()
                self.isLearned.toggle()
                removing?()
                currentlySelectedCard?.cardStatus = 1
                print("Button IsLearned toggled()")
                
            }) {
                Textual content("👍")
                    .body(width: 70, peak: 50)
                    .background(Coloration("Simple"))
                    .clipShape(RoundedRectangle(cornerRadius: 8))
            }
            
            .padding(.trailing, 20)
        }
    }
    func handleButtonTap(card: FlashCardData) {
            if isLearned {
                removing?() // Name the removing closure to take away the cardboard
                print("Realized situation")
            } else if isThink {
                // Deal with the Suppose button faucet
                // Replace card standing and take away if crucial
                // Instance: card.cardStatus = 2
                removing?() // Name the removing closure to take away the cardboard
            } else if isHard {
                // Deal with the Arduous button faucet
                // Replace card standing and take away if crucial
                // Instance: card.cardStatus = 3
                removing?() // Name the removing closure to take away the cardboard
            } else if isRepeat {
                // Deal with the Repeat button faucet
                // Replace card standing and take away if crucial
                // Instance: card.cardStatus = 4
                removing?() // Name the removing closure to take away the cardboard
            }
        }
}

This is single card view:

struct SingleFlashCard: View {
    let card: FlashCardData
    var removing: (() -> Void)? = nil
    
    @State var time period = ""
    @State var definition = ""
    @Binding var isLearned: Bool
    @Binding var isThink: Bool
    @Binding var isHard: Bool
    @Binding var isRepeat: Bool
    
    var physique: some View {
        ZStack {
            RoundedRectangle(cornerRadius: 25, model: .steady)
                .fill(Coloration.white)
            //.offset(x: isLearned ? 500 : 0)
            //.overlay(RoundedRectangle(cornerRadius: 25).stroke(getColor(), lineWidth: 2))// Right here we modify the border coloration based mostly on the swipe path
                .shadow(radius: 3)
            
            VStack {
                NavigationStack {
                    
                    Textual content(card.time period ?? "Unnamed Card")
                        .offset(y: -100)
                    Divider()
                    if isTapped {
                        Textual content(card.definition ?? "Unnamed Card")
                            .offset(y: 100)
                    }
                }
                
                
            }
            
            
        }
        .body(width: 300, peak: 500)
    }
}

Listed below are the core information entities screenshots:
FlashCardData
FlashSets

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments