基于坐标系计算
This commit is contained in:
parent
e784d46299
commit
276ab30a07
@ -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 {
|
||||
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user