I'm trying to animate in a view the bottom of its parent view. This is relatively easy to do by animating the offset, like so:
struct ContentView: View {
@State var isShowingBanner = true
var bannerOffset: CGFloat {
isShowingBanner ? 0 : 60
}
var body: some View {
VStack {
VStack {
Spacer()
BannerView()
.offset(y: bannerOffset)
}
.border(Color.black, width: 1.0)
.clipped()
Spacer()
Button("Toggle Banner") {
withAnimation {
isShowingBanner.toggle()
}
}
}
.padding()
}
}
The glaringly obvious problem is that this just uses an arbitrary value for the animated offset, and this quickly breaks when considering dynamic type
My question is:
Is there a way to properly determine the height of BannerView
to correctly adjust this animation. Or is there a better way to achieve this effect?
Thanks all
It can be done just with transition, like
Tested with Xcode 13.3 / iOS 15.4
struct ContentView: View {
@State var isShowingBanner = true
var body: some View {
VStack {
VStack {
Spacer()
if isShowingBanner {
BannerView()
.transition(.move(edge: .bottom)) // << here !!
}
}
// >> empty container should not shrink !!
.frame(maxWidth: .infinity, maxHeight: .infinity)
.border(Color.black, width: 1.0)
.clipped()
Spacer()
Button("Toggle Banner") {
withAnimation {
isShowingBanner.toggle()
}
}
}
.padding()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With