从滚动视图中的不同视图设置滚动位置

Set scroll position from a different view in scrollview

如何从不同的视图中单击按钮并在不同的视图中设置滚动位置?以下代码会引发错误。我正在使用 environmentalObject。我期望的是,当我单击 ButtonView 中的按钮时,ScrollView1 的滚动位置应该设置为 scrollview 中的 3rd object or 1st object。在同一个视图下效果很好,但是在不同的视图下不知道如何做。

错误:Type '()' cannot conform to 'View'

import SwiftUI

@EnvironmentObject private var environmentVariables: miscellaneousEnvironmentalVariable

struct ScrollView1: View {
var body: some View {
    
    ScrollView {
        
   
        ScrollViewReader { value in
            
            if (environmentVariables.CurrentIndexforPosts >= 0) {
                withAnimation {
                    value.scrollTo((environmentVariables.CurrentIndexforPosts), anchor: .top)}
            }
          
                            
            LazyVStack {
                Text ("TEXT 1")
                Text ("TEXT 2")
                Text ("TEXT 3")
                Text ("TEXT 4")
            }

}
        

struct ButtonView: View {
            var body: some View {
                
                Button("Go to next") {
                    environmentVariables.CurrentIndexforPosts = 3
                                }
                            }
                            .padding()
                   
                  Button("Go to previous") {
                      environmentVariables.CurrentIndexforPosts = 1
                                  }
                              }
                              .padding()
            }
            
        }

多么可怕的MRE!!这就是您的意思及其工作原理:
一。你必须把 .id()s 放在你的 ScrollView 上,否则 scollTo 不知道去哪里。
二。 ScrollView 中必须有足够的条目。如果全部可见,则 scrollTo 不会执行任何操作。

class EnvironmentVariables: ObservableObject {
    @Published var currentIndexforPosts: Int = 0
}


struct ContentView: View {
    
    @StateObject  var environmentVariables = EnvironmentVariables()
    
    var body: some View {
        VStack {
            ButtonView()
            ScrollView1()
        }
        .environmentObject(environmentVariables)
    }
}


struct ScrollView1: View {
    
    @EnvironmentObject var environmentVariables: EnvironmentVariables
    
    var body: some View {
        
        ScrollView {
            ScrollViewReader { value in
                LazyVStack {
                    ForEach(0..<100) { i in
                        Text ("Text \(i)").id(i)
                            .font(.title)
                    }
                }
                
              .onChange(of: environmentVariables.currentIndexforPosts) { _ in
                    if environmentVariables.currentIndexforPosts >= 0 {
                        withAnimation {
                            value.scrollTo(environmentVariables.currentIndexforPosts, anchor: .top)
                        }
                    }
                }
            }
        }
    }
}
            
struct ButtonView: View {
    
    @EnvironmentObject var environmentVariables: EnvironmentVariables

    var body: some View {
        
        Button("Go to next") {
            environmentVariables.currentIndexforPosts += 10
        }
        .padding()
        
        Button("Go to previous") {
            environmentVariables.currentIndexforPosts -= 10
        }
    }
}