Skip to content

Instantly share code, notes, and snippets.

@mattyoung
Last active June 29, 2021 11:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattyoung/c63ea82495b3ad2d418a3997cc6509b5 to your computer and use it in GitHub Desktop.
Save mattyoung/c63ea82495b3ad2d418a3997cc6509b5 to your computer and use it in GitHub Desktop.
private struct OnFirstAppear: ViewModifier {
let perform: () -> Void
let `else`: () -> Void
@State private var firstTime = true
func body(content: Content) -> some View {
content.onAppear {
if firstTime {
firstTime = false
perform()
} else {
`else`()
}
}
}
}
private struct OnNotFirstAppear: ViewModifier {
let perform: () -> Void
@State private var firstTime = true
func body(content: Content) -> some View {
content.onAppear {
if firstTime {
firstTime = false
} else {
perform()
}
}
}
}
extension View {
func onFirstAppear(perform: @escaping () -> Void, else: @escaping () -> Void = { }) -> some View {
modifier(OnFirstAppear(perform: perform, else: `else`))
}
func onNotFirstAppear(perform: @escaping () -> Void) -> some View {
modifier(OnNotFirstAppear(perform: perform))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment