【发布时间】:2022-10-13 03:17:36
【问题描述】:
我在 ScrollView 中有一个视图,并基于偏移值。还有另一种视图需要动画化。
此视图位于灰色框下方,但它的基础是硬编码值。如何在没有硬编码值的情况下将绿色框固定在灰色区域下方?使用硬编码值将与其他设备不一致。
struct ContentView: View {
@State private var contentOffset = CGFloat(0)
var body: some View {
NavigationView {
ZStack {
VStack(spacing: 0) {
TrackableScrollView { offset in
contentOffset = offset.y
} content: {
VStack(spacing: 0) {
Text("Hello World")
.padding()
.frame(maxWidth: .infinity)
.background(Color.gray)
}
}
}
Text("HELLO")
.frame(maxWidth: .infinity)
.background(Color.green)
.opacity(contentOffset < -16 ? 0 : 1)
.animation(.easeIn(duration: 0.2), value: contentOffset)
.offset(y: -280)
}
.ignoresSafeArea()
.frame(maxHeight: .infinity, alignment: .top)
.background(AccountBackground())
.navigationBarHidden(true)
}
}
}
它看起来像什么:
当您向上滚动时,绿色框将消失。
【问题讨论】: