0

Why the geometryreader always prints current index 1? The expected functionality is to print the shown object id when the user scrolls. But it does not change, what could be the issue? The current index should change when user scrolls without pressing the buttons.

struct SwiftUIView: View {
    {

         ScrollView {


            ScrollViewReader { value in
                         Button("Go to next") {
                             withAnimation {
                             value.scrollTo((currentIndex), anchor: .top)
                                currentIndex += 1
                                 print (currentIndex)
                             }
                         }
                         .padding()

                Button("Go to previous") {
                    withAnimation {
                    value.scrollTo((currentIndex), anchor: .top)
                       currentIndex -= 1
                        print (currentIndex)
                    }
                }
                .padding()

                VStack {
                ForEach(0..<12, id: \.self) { obj in

                            PostCard(message:message1)

                              .onAppear{
                                  print(">> added \(obj)")
                                   current.append(obj)

                                 }

                              .onDisappear {
                                 current.removeAll { $0 == obj }
                                   print("<< removed \(obj)")
                                      }.id(obj)
                         }
            }

                .background(GeometryReader {
                Color.clear.preference(key: ViewOffsetKey.self,
                    value: -$0.frame(in: .named("scroll")).origin.y)
})
.onPreferenceChange(ViewOffsetKey.self) {_ in
    if current.count > 0 {
     currentIndex = current.sorted()[1]
        print("current index \(String (currentIndex))")
    }
     }
          }
      }
       }

        }
        }


        }
    }
}
TheDummy
  • 65
  • 7
  • Does this answer your question https://stackoverflow.com/a/62588295/12299030? – Asperi Feb 06 '22 at 15:14
  • @Asperi I don't get it how it relates to this question. I'm asking about detecting scroll and do the action. That question just reads the position etc? – TheDummy Feb 06 '22 at 15:40
  • shouldn't it be easy as ".OnScroll {do some action}" ? – TheDummy Feb 06 '22 at 15:40
  • it should, but it isn't. Despite the name – with ScrollViewReader you can only scroll to a position, but not read the position, neither detect scrolling. So with @Asperis answer you CAN read the position, see if it changes and then do something. – ChrisR Feb 06 '22 at 16:11
  • @Asperi I have updated the question according to the links given. But now the `currentIndex` always prints 1. why is that – TheDummy Feb 06 '22 at 16:28
  • See [this answer](https://stackoverflow.com/a/69500827/7129318) after the edit. There is code in there to detect whether the `ScrollView` is scrolling by using a `PreferenceKey` and a `Combine Publisher` that will do what you want. – Yrb Feb 06 '22 at 16:30
  • @Yrb I don't get what `combine publisher` means and in my code I have used the `preferencekey` – TheDummy Feb 07 '22 at 12:48
  • There are many, many articles posted about Combine. The code's function that I directed you to is to determine whether the user is scrolling or not, and, if not it will read the cell rows on the screen, and then cause the last row to be scrolled until it is fully shown on the screen. It also adjusts the rows when the keyboard appears on screen so the last row is still shown regardless of keyboard state. It does everything you are looking for in this question, plus the keyboard state. – Yrb Feb 07 '22 at 13:01

0 Answers0