-
Notifications
You must be signed in to change notification settings - Fork 2
/
TimeLineAnimation.swift
100 lines (88 loc) · 3.24 KB
/
TimeLineAnimation.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// TimeLineAnimationView.swift
// L-Swift
//
// Created by xqsadness on 14/06/2024.
//
import SwiftUI
struct TLModel: Identifiable{
var id = UUID()
var title: String
var isCompleted: Bool
}
struct TimeLineAnimationView: View {
@State private var list: [TLModel] = [
TLModel(
title: "Hello world",
isCompleted: false
),
TLModel(
title: "SwiftUI Animation",
isCompleted: false
),
TLModel(
title: "Xcode 14+, IOS 16+",
isCompleted: false
)
]
var body: some View {
LazyVStack(spacing: 0){
ForEach(list.indices, id: \.self) { item in
let todo = list[item]
HStack(spacing: 15){
Image(systemName: todo.isCompleted ? "checkmark.circle.fill" : "circle")
.imageScale(.medium)
.foregroundStyle(todo.isCompleted ? .green : .text)
Text(todo.title)
.strikethrough(todo.isCompleted)
.lineLimit(1)
.animation(.none, value: todo.isCompleted)
Spacer()
}
.overlay(alignment: .topLeading) {
Rectangle()
.frame(width: 1, height: todo.isCompleted ? 53 : 0)
.offset(y: 20)
.padding(.leading, 9)
}
.frame(height: 70)
.animation(.bouncy, value: todo.isCompleted)
.hAlign(.leading)
.contentShape(Rectangle())
.onTapGesture {
withAnimation {
list[item].isCompleted.toggle()
}
}
}
VStack{
if !list.isEmpty{
HStack(spacing: 15){
Image(systemName: list.allSatisfy({$0.isCompleted}) ? "checkmark.circle.fill" : "circle")
.imageScale(.medium)
Text("Finish")
Spacer()
}
.foregroundStyle(list.allSatisfy({$0.isCompleted}) ? .green : .gray)
.frame(height: 46, alignment: .bottom)
.animation(.bouncy, value: list.allSatisfy({$0.isCompleted}))
.onTapGesture {
let allTrue = list.allSatisfy({$0.isCompleted})
let delayStep = 0.5
let indices = allTrue ? Array(list.indices.reversed()) : Array(list.indices)
for (offset , item) in indices.enumerated(){
let delay = Double (offset) * delayStep
DispatchQueue.main.asyncAfter (deadline: .now() + delay) {
self.list[item].isCompleted = !allTrue
}
}
}
}
}
}
.padding(.horizontal)
}
}
#Preview {
TimeLineAnimationView()
}