我在一个日历应用程序上工作,它允许管理日常约会。我使用一个惰性网格来显示当天活跃合作者的一列。约会从核心数据加载到这些列中。约会在 VStack 中。我用 y 偏移和底部填充定位它们。您可以在我加入的日历捕获日历捕获中看到结果。
它运作良好,直到我有重叠的约会。只要预约的结束时间不超过或等于下一次的结束时间,就没有问题
我的专栏构建方法:
Group {
if let appointments = columnViewModel.columnCollab.todaySortedAppointments(for: day),
let dayHours = columnViewModel.columnCollab.collabHours.first(where: {$0.dayID == day.dayOfWeek()}),
dayHours.active {
// allow to receive onDrop
Color.black.opacity(0.001)
VStack(spacing: 0) {
dayBeginning(collabHours: dayHours).zIndex(1)
if appointments.count == 0 {
Spacer().zIndex(1)
} else {
ForEach(appointments, id: \.id) { item in
AppointmentView(rdv: item,
viewColor: Color.green,
viewHeight: appointmentHeight(appointment: item))
.offset(y: appointmentOffset(item: item, collabHours: dayHours))
.padding(.bottom, appointmentPadding(item: item, collabHours: dayHours))
}
}
Spacer(minLength: 0)
dayEnding(collabHours: dayHours).zIndex(1)
}.frame(height: Constants.dayHeight, alignment: .topLeading)
} else {
VStack {
Spacer()
}
}
}
我的偏移量,填充计算:
private func appointmentOffset(item: Appointment, collabHours: DailyHours) -> CGFloat {
let startDay = currentDay.getDateFrom(hour: collabHours.startHour, minutes: collabHours.startMinute)
let previous = columnViewModel.columnCollab.todaySortedAppointments(for: currentDay).before(item)
var appointmentDuration = item.startDate!.getMinutesDuration(from: previous?.endDate ?? startDay)
if previous?.startDate == item.startDate {
return 0
} else {
var offset = ((CGFloat(appointmentDuration) * Constants.hourHeight)/60)
return offset
}
}
private func appointmentPadding(item: Appointment, collabHours: DailyHours) -> CGFloat {
let startDay = currentDay.getDateFrom(hour: collabHours.startHour, minutes: collabHours.startMinute)
let previous = columnViewModel.columnCollab.todaySortedAppointments(for: currentDay).before(item)
var appointmentDuration = item.startDate!.getMinutesDuration(from: previous?.endDate ?? startDay)
if previous?.endDate == item.endDate {
return 0
} else {
var padding = ((CGFloat(appointmentDuration) * Constants.hourHeight)/60)
return padding
}
}
使用这些偏移和填充方法,如果有重叠,则下一个约会将向下移动,并且重叠的约会不可见。如何着手避免这种位移并保持所有约会可见?