Skip to content

Instantly share code, notes, and snippets.

@Edudjr
Last active October 24, 2022 15:59
Show Gist options
  • Save Edudjr/7868167f080cd4b2c55bbde775c36911 to your computer and use it in GitHub Desktop.
Save Edudjr/7868167f080cd4b2c55bbde775c36911 to your computer and use it in GitHub Desktop.
struct AnimationSample: View {
@State var isAnimated = false
@State var displaySheet = false
var body: some View {
Text("Testing")
.scaleEffect(isAnimated ? 2 : 1)
.onTapGesture {
Task {
await animate(duration: 0.5) {
isAnimated = true
}
displaySheet = true
}
}
.sheet(isPresented: $displaySheet) {
Text("Another view!")
}
}
}
extension View {
func animate(duration: CGFloat, _ execute: @escaping () -> Void) async {
await withCheckedContinuation { continuation in
withAnimation(.linear(duration: duration)) {
execute()
}
DispatchQueue.main.asyncAfter(deadline: .now() + duration) {
continuation.resume()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment