Skip to content

Instantly share code, notes, and snippets.

@chrismays
Forked from mattyoung/OnNotFirstTime.swift
Created June 14, 2021 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrismays/90a1984dfaecc8b409d1c099348b286c to your computer and use it in GitHub Desktop.
Save chrismays/90a1984dfaecc8b409d1c099348b286c 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