ios - 在滚动视图中查找最高的可见视图 [SwiftUI]
问题描述
我试图在ScrollView
. 对于背景,我正在制作一个可滚动的条形图。这是我到目前为止得到的,但onDisappear
没有被调用。
private var highestVisibleAmount: Double {
visibleDates
.compactMap { viewModel.transactions[$0] }
.map {
$0.map{ $0.amount }
.reduce(0, +)
}
.max() ?? 0
}
@State var visibleDates: Set<Date> = []
var body: some View {
ScrollView(Axis.Set.horizontal, showsIndicators: false) {
ScrollViewReader { reader in
LazyHStack(alignment: .bottom, spacing: 1) {
ForEach(viewModel.transactions.keys.sorted(), id: \.self) { date in
let transactions = viewModel.transactions[date] ?? []
BarView(viewModel: BarView.ViewModel(transactions: transactions, highestVisibleAmount: highestVisibleAmount))
.frame(width: viewModel.segmentLength)
.onAppear(perform: { visibleDates.insert(date)})
.onDisappear(perform: { visibleDates.remove(date)})
}
}
}
}
.frame(height: viewModel.chartHeight)
}
解决方案
推荐阅读
- java - 导航回同一页面后无法再次单击随机链接 - 硒自动化
- authorization - 如何获得 XACML 政策许可?
- apache-spark - 拆分 .ttl 或 .nt 文件 - Spark Scala
- c# - 如何手动设置 HttpContext.User.Identity.IsAuthenticated 的值
- php - 具有多个一对一关系的 Laravel 用户模型
- ios - 从点击手势识别器获取标签
- matlab - 更改对象在八度音程/ matlab 中完成旋转所需的时间长度/持续时间
- php - PHPOffice 写入新文件致命错误
- c# - 保存页面路径的数据类型
- c# - 如何在 Utc 中获取 DateTime 格式?