From 276ab30a078b9abe36fab85f78fba61e33fc3dd4 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Tue, 10 Jun 2025 21:08:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E4=BA=8E=E5=9D=90=E6=A0=87=E7=B3=BB?= =?UTF-8?q?=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dimensionhub/Views/Index/IndexMainView.swift | 5 ++- dimensionhub/Views/Index/IndexModel.swift | 47 +++++++++++++++++--- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/dimensionhub/Views/Index/IndexMainView.swift b/dimensionhub/Views/Index/IndexMainView.swift index f6548d2..f070aac 100644 --- a/dimensionhub/Views/Index/IndexMainView.swift +++ b/dimensionhub/Views/Index/IndexMainView.swift @@ -79,13 +79,13 @@ struct IndexMainView: View { LazyVStack(alignment: .center, spacing: 10) { ForEach(Array(indexModel.dramaGroupElements.enumerated()), id: \.offset) { idx, item in switch item { - case .label(let groupId, let groupName): + case .label(let groupId, let groupName, _): DramaGroupLabelView(group_name: groupName) { selectGroupId = groupId indexModel.selectedDate = groupId showDateNavPopover = true } - case .item(let groupId, let item): + case .item(let groupId, let item, _): DramaGroupItemView(groupId: groupId, item: item) .id(idx) } @@ -100,6 +100,7 @@ struct IndexMainView: View { .scrollPosition(id: $scrollID) .onChange(of: scrollOffset) { self.showFloatGroupLabel = scrollOffset < 0 + indexModel.offsetChanged(offset: scrollOffset) } .refreshable { guard !self.showDateNavPopover && !self.headerRefreshing else { diff --git a/dimensionhub/Views/Index/IndexModel.swift b/dimensionhub/Views/Index/IndexModel.swift index 681ab6c..17b14c8 100644 --- a/dimensionhub/Views/Index/IndexModel.swift +++ b/dimensionhub/Views/Index/IndexModel.swift @@ -52,9 +52,9 @@ final class IndexModel { enum GroupElement { // groupId, groupName - case label(String, String) + case label(String, String, Double) // groupId, UpdateDramaGroup.Item - case item(String, UpdateDramaGroup.Item) + case item(String, UpdateDramaGroup.Item, Double) } var selectedDate: String @@ -95,6 +95,10 @@ final class IndexModel { @ObservationIgnored private var cancel: AnyCancellable? + // 定义几个关键元素的高度 + @ObservationIgnored + private var elementHeight: (Double, Double, Double) = (10, 21.6, 180) // spacing: 10, label height: 21.6, item: 180 + init() { self.selectedDate = "" @@ -102,9 +106,9 @@ final class IndexModel { .sink { userId, scrollID in // 更新group的label的部分 switch self.dramaGroupElements[scrollID] { - case .label(_, _): + case .label(_, _, _): () - case .item(let groupId, _): + case .item(let groupId, _, _): DispatchQueue.main.async { self.setFixedDrameGroup(groupId: groupId) } @@ -122,6 +126,32 @@ final class IndexModel { } } + // 位置变化 + func offsetChanged(offset: Double) { + // 判断当前的offset在那个视图所在的区间 + var rangeGroupId: String? = nil + let (spacing, labelHeight, elementHeight) = self.elementHeight + + self.dramaGroupElements.forEach { element in + switch element { + case .label(let groupId, _, let y): + if offset + y >= -labelHeight && offset + y <= 0 { + rangeGroupId = groupId + } + case .item(let groupId, _, let y): + if offset + y >= -elementHeight && offset + y <= 0 { + rangeGroupId = groupId + } + } + } + + if let rangeGroupId { + DispatchQueue.main.async { + self.setFixedDrameGroup(groupId: rangeGroupId) + } + } + } + func loadData(userId: String) async { guard !isLoaded else { return @@ -219,11 +249,16 @@ final class IndexModel { // 转换成显示的元素信息 private func transformUpdateDramaGroups(groups: [UpdateDramaGroup]) -> [GroupElement] { + var offset: Double = 0 + let (spacing, labelHeight, elementHeight) = self.elementHeight + var groupElements: [GroupElement] = [] for group in groups { - groupElements.append(.label(group.group_id, group.group_name)) + groupElements.append(.label(group.group_id, group.group_name, offset)) + offset += labelHeight + spacing for item in group.items { - groupElements.append(.item(group.group_id, item)) + groupElements.append(.item(group.group_id, item, offset)) + offset += elementHeight + spacing } }