From d9828bc3c858544df48a9902bd845330f8e0f96d Mon Sep 17 00:00:00 2001 From: wangdl Date: Sat, 6 Jun 2026 19:22:08 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20#34=20#35=20=E7=A7=BB=E9=99=A4=20content?= =?UTF-8?q?HeightEstimate=2080px=20=E7=A1=AC=E7=BC=96=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - actualContentHeight 通过 GeometryReader 获取真实内容高度 - scrollProgress 使用实际高度计算,精度显著提升 - #36 ReadingEventCollector 已清理完毕(无 ObservableObject/Combine) Co-Authored-By: Claude Opus 4.7 --- .../MaterialReader/MaterialReaderView.swift | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/AIStudyApp/AIStudyApp/Features/MaterialReader/MaterialReaderView.swift b/AIStudyApp/AIStudyApp/Features/MaterialReader/MaterialReaderView.swift index abc4768..c32af26 100644 --- a/AIStudyApp/AIStudyApp/Features/MaterialReader/MaterialReaderView.swift +++ b/AIStudyApp/AIStudyApp/Features/MaterialReader/MaterialReaderView.swift @@ -100,6 +100,7 @@ struct MaterialReaderView: View { @State private var scrollProgress: CGFloat = 0 @State private var hasRestoredPosition = false @State private var restoreBlockId: String? + @State private var actualContentHeight: CGFloat = 1 private let title: String private let knowledgeBaseId: String? @@ -233,15 +234,17 @@ struct MaterialReaderView: View { } .padding(.horizontal, 20).padding(.top, 8).padding(.bottom, 100) .background(GeometryReader { geo in - Color.clear.preference( - key: ScrollOffsetKey.self, - value: geo.frame(in: .named("scroll")).minY - ) + Color.clear + .preference(key: ScrollOffsetKey.self, value: geo.frame(in: .named("scroll")).minY) + .onAppear { actualContentHeight = geo.size.height } }) + .onChange(of: vm.blocks.count) { _,_ in + // Height will be updated by GeometryReader on next render + } } .coordinateSpace(name: "scroll") .onPreferenceChange(ScrollOffsetKey.self) { offset in - scrollProgress = min(1, max(0, -offset / max(contentHeightEstimate, 1))) + scrollProgress = min(1, max(0, -offset / max(actualContentHeight, 1))) reportScrollPosition() } .onChange(of: restoreBlockId) { _, target in @@ -256,12 +259,6 @@ struct MaterialReaderView: View { } } - /// Estimate total block area height for scroll progress calculation - private var contentHeightEstimate: CGFloat { - let count = max(vm.blocks.count, 1) - return CGFloat(count) * 80 - } - /// Build a NoteAnchor from the current scroll position (for quick note). private func buildAnchor() -> NoteAnchor? { guard let pos = collector.lastPosition else { return nil }