Skip to content

Instantly share code, notes, and snippets.

@allfinlir
Created July 11, 2023 20:16
Show Gist options
  • Save allfinlir/c2c6c89655af07d5701f19db2e039588 to your computer and use it in GitHub Desktop.
Save allfinlir/c2c6c89655af07d5701f19db2e039588 to your computer and use it in GitHub Desktop.
import SwiftUI
struct RepeatingViews: View {
@State private var disappearEast: Bool = false
@State private var appearEast: Bool = false
@State private var moveEast: Bool = false
@State private var disappearSugarBeet: Bool = false
@State private var appearSugarBeet: Bool = false
@State private var moveSugarBeet: Bool = false
@State private var disappearCarrot: Bool = false
@State private var appearCarrot: Bool = false
@State private var moveCarrot: Bool = false
let repeatView = Timer.publish(every: 4.50, on: .main, in: .common).autoconnect()
var body: some View {
VStack(spacing: 100) {
VStack {
Text("EAST")
.padding(5)
.background(Color.yellow)
.opacity(disappearEast ? 0 : 1)
.scaleEffect(disappearEast ? 0 : 1)
.offset(y: moveEast ? 118 : 0)
.offset(y: moveSugarBeet ? -59 : 0)
.offset(y: moveCarrot ? -59 : 0)
Text("Sugar Beet")
.padding(5)
.background(Color.green)
.opacity(disappearSugarBeet ? 0 : 1)
.scaleEffect(disappearSugarBeet ? 0 : 1)
.offset(y: moveEast ? -59 : 0)
.offset(y: moveSugarBeet ? 118 : 0)
.offset(y: moveCarrot ? -59 : 0)
Text("Carrot")
.padding(5)
.background(Color.orange)
.opacity(disappearCarrot ? 0 : 1)
.scaleEffect(disappearCarrot ? 0 : 1)
.offset(y: moveEast ? -59 : 0)
.offset(y: moveSugarBeet ? -59 : 0)
.offset(y: moveCarrot ? 118 : 0)
}
.font(.largeTitle)
}
.onReceive(repeatView, perform: { _ in
startAnimation()
})
.onAppear {
startAnimation()
}
}
func startAnimation() {
withAnimation(.easeInOut(duration: 0.5)) {
disappearEast = true
}
withAnimation(.easeInOut(duration: 0.5).delay(0.5)) {
moveEast = true
}
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
withAnimation(.easeInOut(duration: 0.5)) {
disappearEast = false
}
}
withAnimation(.easeInOut(duration: 0.5).delay(1.5)) {
disappearSugarBeet = true
}
withAnimation(.easeInOut(duration: 0.5).delay(2)) {
moveSugarBeet = true
}
DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) {
withAnimation(.easeInOut(duration: 0.5)) {
disappearSugarBeet = false
}
}
withAnimation(.easeInOut(duration: 0.5).delay(3)) {
disappearCarrot = true
}
withAnimation(.easeInOut(duration: 0.5).delay(3.5)) {
moveCarrot = true
}
DispatchQueue.main.asyncAfter(deadline: .now() + 4) {
withAnimation(.easeInOut(duration: 0.5)) {
disappearCarrot = false
}
}
DispatchQueue.main.asyncAfter(deadline: .now() + 4.5) {
moveEast = false
moveSugarBeet = false
moveCarrot = false
}
}
}
struct RepeatingViews_Previews: PreviewProvider {
static var previews: some View {
RepeatingViews()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment