Saturday, October 1, 2022
HomeiOS Developmentios - SwiftUI ScrollViewReader scrollTo not working as anticipated

ios – SwiftUI ScrollViewReader scrollTo not working as anticipated


I’m constructing a piece of my app that enables customers to coach their expertise in a sport by aiming for targets. Customers can see their targets in an inventory. If greater than 1 individuals are utilizing this characteristic in my app, their coaching goal lists are put subsequent to one another in a scroll view. I would like my app to robotically scroll to the at present energetic person. The code I take advantage of for that is:

func getTrainingTargetsScrollView(dimension: CGSize) -> some View {
    let gamers: [DSPlayer] = trainingController.currentExercise?.gamers ?? []
    
    return ScrollViewReader { worth in
        ScrollView(.horizontal) {
            HStack(spacing: 0) {
                ForEach(gamers, id: .id) { participant in
                    ExerciseTable(participant: participant)
                        .body(width: dimension.width)
                }
            }
        }
        .onReceive(trainingController.$currentPlayer, carry out: { newValue in
            if let id = newValue?.id {
                withAnimation {
                    worth.scrollTo(id)
                }
            }
        })
        .body(peak: dimension.peak - 130)
    }
}

the onReceive will get triggered and I’ve verified that the gamers it will get handed are the precise ones and have matching ID’s with the gamers within the scrollview. Nonetheless, it merely doesn’t scroll.

Can anybody see what I am doing fallacious?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments