偏移动画滞后

时间:2021-05-31 10:26:17

标签: animation swiftui lag

当向上滚动超出“常规滚动视图”时,我希望导航栏贴在滚动视图上。我为此使用了 .offset()GeometryReader 并且它正在工作。但是,导航栏明显滞后:Video

是否有另一种方法可以实现粘性导航栏或可以对此进行更改的方法?我是否使用了太多视图?

struct V_Home: View {
    
    var previewData = PreviewData()
    @State var size: CGRect = .zero
    
    var body: some View {
        
        GeometryReader { geometry in
            ZStack {
                ScrollView {
                    VStack {
                        
                        // used to read the scroll position
                        GeometryReader { proxy in
                            Color.clear
                                .preference(key: SizePreferenceKey.self, value: proxy.frame(in: .named("scrollView")))
                        }
                        .frame(height: 0)
                        .onPreferenceChange(SizePreferenceKey.self) { preferences in
                                    self.size = preferences
                        }
                        
                        // List
                        ForEach(previewData.ScoreSessionList) { scoreSession in
                            NavigationLink(destination: V_SessionDetail(scoreSession: scoreSession)) {
                                HStack(spacing: 0) {
                                    V_ScoreSessionListItem(scoreSession: scoreSession)
                                }
                            }.padding(.top, 10)
                        }
                        .padding([.leading, .trailing], 25)
                    }
                }
                .coordinateSpace(name: "scrollView")
                
                // NavBar
                VStack {
                    // This Rectangle is offset to match the scroll position
                    // Is is lagging behind noticably
                    Rectangle()
                        .fill(Color(.green))
                        .frame(height: 80)
                    .offset(y: self.size.minY > 0 ? self.size.minY : 0)
                    .padding(0)
                    
                    Spacer()
                }
            }
            .edgesIgnoringSafeArea(.all)
            .navigationBarTitle("")
            .navigationBarHidden(true)
        }
    }
}

// used to make scrollview position accessible to other view
struct SizePreferenceKey: PreferenceKey {
    typealias Value = CGRect
    static var defaultValue: Value = .zero

    static func reduce(value _: inout Value, nextValue: () -> Value) {
        _ = nextValue()
    }
}

0 个答案:

没有答案
相关问题