Skip to content

Instantly share code, notes, and snippets.

@allfinlir
Created September 23, 2022 07:06
Show Gist options
  • Save allfinlir/97d01aedae44f83006de09b80b0f5782 to your computer and use it in GitHub Desktop.
Save allfinlir/97d01aedae44f83006de09b80b0f5782 to your computer and use it in GitHub Desktop.
import SwiftUI
struct BoxesLoadingView: View {
@State private var moveBoxeDown = false
@State private var moveBoxRight = false
@State private var moveBoxUp = false
@State private var moveAllBoxesLeft = false
@State private var refreshPage = false
var body: some View {
ZStack {
VStack(spacing: 0) {
HStack(spacing: 8) {
Rectangle()
.frame(width: 30, height: 30)
.foregroundColor(.gray)
.offset(y: moveBoxeDown ? 38 : 0)
.offset(x: moveBoxRight ? 114 : 0)
.offset(y: moveBoxUp ? -38 : 0)
Rectangle()
.frame(width: 30, height: 30)
.foregroundColor(.gray)
Rectangle()
.frame(width: 30, height: 30)
.foregroundColor(.gray)
}
.offset(x: moveAllBoxesLeft ? -38 : 0)
}
}
.onTapGesture {
withAnimation(.easeInOut(duration: 0.6).delay(0.3)) {
moveBoxeDown = true
}
withAnimation(.easeInOut(duration: 1.4).delay(0.9)) {
moveBoxRight = true
}
withAnimation(.easeInOut(duration: 0.6).delay(2.3)) {
moveBoxUp = true
}
withAnimation(.easeInOut(duration: 1).delay(2.9)) {
moveAllBoxesLeft = true
}
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
moveBoxeDown = false
moveBoxRight = false
moveBoxUp = false
moveAllBoxesLeft = false
}
}
}
}
struct BoxesLoadingView_Previews: PreviewProvider {
static var previews: some View {
BoxesLoadingView()
}
}
@akardas16
Copy link

akardas16 commented Sep 23, 2022

//
//  BoxesLoadingView.swift
//  Widget Demo App
//
//  Created by Abdullah Kardas�� on 23.09.2022.
//

import SwiftUI

struct BoxesLoadingView: View {
    @State private var moveBoxeDown = false
    @State private var moveBoxRight = false
    @State private var moveBoxUp = false
    @State private var moveAllBoxesLeft = false
    @State private var refreshPage = false
    
    
    let timerAnim = Timer.publish(every: 4.0, on: .main, in: .common).autoconnect()
    
    var body: some View {
        ZStack {
            VStack(spacing: 0) {
                
                HStack(spacing: 8) {
                    Rectangle()
                        .frame(width: 30, height: 30)
                        .foregroundColor(.gray)
                        .offset(y: moveBoxeDown ? 38 : 0)
                        .offset(x: moveBoxRight ? 114 : 0)
                        .offset(y: moveBoxUp ? -38 : 0)
                    
                    Rectangle()
                        .frame(width: 30, height: 30)
                        .foregroundColor(.gray)
                    
                    Rectangle()
                        .frame(width: 30, height: 30)
                        .foregroundColor(.gray)
                }
                .offset(x: moveAllBoxesLeft ? -38 : 0)
                
                
            }
        }.onReceive(timerAnim, perform: { _ in
            yourAnims()
        }).onAppear {
            yourAnims()
        }
       
    }
    
    func yourAnims(){
        withAnimation(.easeInOut(duration: 0.6).delay(0.3)) {
            moveBoxeDown = true
        }
        
        withAnimation(.easeInOut(duration: 1.4).delay(0.9)) {
            moveBoxRight = true
        }
        
        withAnimation(.easeInOut(duration: 0.6).delay(2.3)) {
            moveBoxUp = true
        }
        
        withAnimation(.easeInOut(duration: 1).delay(2.9)) {
            moveAllBoxesLeft = true
        }
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
            moveBoxeDown = false
            moveBoxRight = false
            moveBoxUp = false
            moveAllBoxesLeft = false
        }
    }
}

struct BoxesLoadingView_Previews: PreviewProvider {
    static var previews: some View {
        BoxesLoadingView()
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment