I'd like to perform an action to hide elements on my screen when a button is tapped using SwiftUI.
So for example, let's say I have 2 images, a text, and a button on my screen.
If I tap the button I want the other elements to animate off-screen. Tap the button again and they animate back onto the screen.
Each element is created in their own View which is in its own file. Is there a way to access a View from another View?
Here's an example:
struct buttonView : View {
@State private var isShowingImageView = false
@Binding var myImage: ImageView
var body: some View {
Button(action: {
self.isShowingImageView.toggle()
}) {
HStack(spacing: 18) {
Image("button-icon")
Text("Press Me")
}
}
if isShowingImageView {
myImage.hidden()
}
}
}
My image is created in its own struct / View and when I press the button I want to hide that image.
Right now it's not hiding it, just the view my button is in animates a little.