From e784d462990f964307c69f72bcb7b2ea4f5e3d9d Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Tue, 10 Jun 2025 18:33:04 +0800 Subject: [PATCH] fix --- dimensionhub/Views/Index/IndexMainView.swift | 111 ++++++++++++++----- dimensionhub/Views/Index/IndexModel.swift | 3 + 2 files changed, 84 insertions(+), 30 deletions(-) diff --git a/dimensionhub/Views/Index/IndexMainView.swift b/dimensionhub/Views/Index/IndexMainView.swift index 6e5c180..f6548d2 100644 --- a/dimensionhub/Views/Index/IndexMainView.swift +++ b/dimensionhub/Views/Index/IndexMainView.swift @@ -29,6 +29,9 @@ struct IndexMainView: View { @State private var footerRefreshing: Bool = false @State private var noMore: Bool = false + @State private var scrollOffset: CGFloat = 0 + @State private var showFloatGroupLabel: Bool = false + var body: some View { VStack(alignment: .center) { @@ -61,8 +64,16 @@ struct IndexMainView: View { .background(Color(hex: "#F2F2F2"), ignoresSafeAreaEdges: .top) ScrollView(.vertical, showsIndicators: false) { - Rectangle() - .frame(width: 0, height: 38) + ScrollViewOffsetReader(offset: $scrollOffset) + + MixGroupLabelView(fixedDramaGroup: indexModel.fixedDramaGroup) { + if let groupId = indexModel.fixedDramaGroup?.group_id { + selectGroupId = groupId + indexModel.selectedDate = groupId + showDateNavPopover = true + } + } + .opacity(!showFloatGroupLabel ? 1 : 0) // 基于日期的更新列表 LazyVStack(alignment: .center, spacing: 10) { @@ -87,6 +98,9 @@ struct IndexMainView: View { .frame(width: 370) .coordinateSpace(name: "indexScrollView") .scrollPosition(id: $scrollID) + .onChange(of: scrollOffset) { + self.showFloatGroupLabel = scrollOffset < 0 + } .refreshable { guard !self.showDateNavPopover && !self.headerRefreshing else { return @@ -107,37 +121,15 @@ struct IndexMainView: View { } } .overlay(alignment: .topTrailing) { - HStack(alignment: .center) { - Button(action: { - appNavigation.append(dest: .search) - }) { - Image(systemName: "magnifyingglass") - .font(.system(size: 20)) + MixGroupLabelView(fixedDramaGroup: indexModel.fixedDramaGroup) { + if let groupId = indexModel.fixedDramaGroup?.group_id { + selectGroupId = groupId + indexModel.selectedDate = groupId + showDateNavPopover = true } - Spacer() - - if let fixedDramaGroup = indexModel.fixedDramaGroup { - Text(fixedDramaGroup.group_name) - .font(.system(size: 18)) - .fontWeight(.regular) - .onTapGesture { - selectGroupId = fixedDramaGroup.group_id - indexModel.selectedDate = fixedDramaGroup.group_id - showDateNavPopover = true - } - } } - .padding([.top, .bottom], 8) - .background(.white) - .background( - GeometryReader { geometry in - Color.clear - .onAppear { - print("hstack height is: \(geometry.size.height)") - } - } - ) + .opacity(showFloatGroupLabel ? 1 : 0) } } .ignoresSafeArea(edges: .bottom) @@ -156,10 +148,69 @@ struct IndexMainView: View { } } + struct ScrollViewOffsetReader: View { + @Binding var offset: CGFloat + + var body: some View { + GeometryReader { geometry in + Color.clear + .preference( + key: ScrollOffsetPreferenceKey.self, + value: geometry.frame(in: .named("indexScrollView")).origin.y + ) + } + .frame(height: 0) + .onPreferenceChange(ScrollOffsetPreferenceKey.self) { value in + DispatchQueue.main.async { + self.offset = value + } + } + } + } + + struct ScrollOffsetPreferenceKey: PreferenceKey { + static var defaultValue: CGFloat = 0 + + static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) { + value = nextValue() + } + } + } extension IndexMainView { + struct MixGroupLabelView: View { + @EnvironmentObject var appNav: AppNavigation + + let fixedDramaGroup: IndexModel.UpdateDramaGroup? + let onTap: () -> Void + + var body: some View { + HStack(alignment: .center) { + Button(action: { + appNav.append(dest: .search) + }) { + Image(systemName: "magnifyingglass") + .font(.system(size: 20)) + } + + Spacer() + + if let fixedDramaGroup { + Text(fixedDramaGroup.group_name) + .font(.system(size: 18)) + .fontWeight(.regular) + .onTapGesture { + onTap() + } + } + } + .padding([.top, .bottom], 8) + .background(.white) + } + } + // 显示分组信息 struct DramaGroupLabelView: View { let group_name: String diff --git a/dimensionhub/Views/Index/IndexModel.swift b/dimensionhub/Views/Index/IndexModel.swift index f17cd64..681ab6c 100644 --- a/dimensionhub/Views/Index/IndexModel.swift +++ b/dimensionhub/Views/Index/IndexModel.swift @@ -65,6 +65,9 @@ final class IndexModel { // 转换后的数据, 方便在一次循环中展示数据 var dramaGroupElements: [GroupElement] = [] + // 判断GroupLabel的显示方式 + var showFloatGroupLabel: Bool = false + var follow_num: String = "0" // 用来显示固定栏目的group_name