下面的代码效果很好,但是几何读取导致我上游出现问题,所以我希望有相同的逻辑来基于可用空间构建视图的宽度,但不使用几何读取器。可能吗?
var body: some View {
ZStack {
GeometryReader { geometry in
ZStack(alignment: .leading) {
RoundedRectangle(cornerRadius: 12.0)
.fill(Color(.lightGray))
.opacity(0.1)
.frame(height: 20)
RoundedRectangle(cornerRadius: 12.0)
.fill(getColorForBar(progress: progress))
.frame(width: getFillWidth(progress: progress, geometry: geometry), height: 20)
.animation(self.progressAnimation)
}
}
}
}
private func getFillWidth(progress: Double, geometry: GeometryProxy) -> CGFloat {
var offset: CGFloat = geometry.size.width * CGFloat(progress)
if progress == 0 {
offset = (geometry.size.width * CGFloat(progress)) + 10.0
} else if progress == 1.0 {
offset = (geometry.size.width * CGFloat(progress)) - 10.0
}
return offset
}