-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlinkCircleView.swift
More file actions
52 lines (48 loc) · 1.68 KB
/
Copy pathBlinkCircleView.swift
File metadata and controls
52 lines (48 loc) · 1.68 KB
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
//
// BlinkCircleView.swift
// AnimationExamples
//
// Created by Esat Gözcü on 2023/03/14.
//
import SwiftUI
struct BlinkCircleView: View {
@StateObject var vm = BlinkCircleVM()
var body: some View {
GeometryReader { geo in
ForEach(0..<vm.getItemSize(), id: \.self) { id in
Circle()
.foregroundColor(.black)
.frame(width: geo.size.width/CGFloat(vm.rowCount), height: geo.size.width/CGFloat(vm.rowCount))
.scaleEffect(vm.currentSize)
.opacity(vm.currentSize)
.animation(.linear.delay(Double(id)*vm.animTime))
.offset(
x: vm.calculateOffsetX(width: geo.size.width, i: id),
y: vm.calculateOffsetY(size: geo.size, i: id)
).id(vm.viewID)
}
}.onAppear{
startAnim()
activeTimer()
}.ignoresSafeArea()
}
private func startAnim(){
withAnimation(.linear(duration: vm.animTime/2).repeatCount(1, autoreverses: false)){
vm.currentSize = vm.endSize
}
DispatchQueue.main.asyncAfter(deadline: .now() + vm.animTime/2) {
withAnimation(.linear(duration: vm.animTime/2).repeatCount(1, autoreverses: false)){
vm.currentSize = vm.startSize
}
}
}
private func activeTimer(){
Timer.scheduledTimer(withTimeInterval: vm.animTime*Double(vm.getItemSize()), repeats: true) { timer in
vm.viewID += 1
vm.rowCount += 1
startAnim()
timer.invalidate()
self.activeTimer()
}
}
}