启用用于不透明度的单独状态变量并独立进行动画,您可以使不透明度取决于
headerOffset
:// ScrollToHideView
// @State private var opacity = 1.0
.safeAreaInset(edge: .top, spacing: 0) {
HeaderView()
.padding(.bottom, 16)
.frame(height: headerHeight, alignment: .bottom)
.background(.blue)
.opacity(1.0 - min(1.0, max(0.0, headerOffset / headerHeight))) // 👈 here
.offset(y: -headerOffset)
}
// animating opacity
// withAnimation(.easeIn(duration: 2)) {
// if self.isScrollingUp {
// opacity = 0
// } else {
// opacity = 1
// }
// }