/
ArcRotationAnimation.swift
69 lines (55 loc) · 2.05 KB
/
ArcRotationAnimation.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
//
// ArcRotationAnimation.swift
// SwiftUIAnimations
//
// Created by Amisha Italiya on 16/01/24.
//
import SwiftUI
struct ArcRotationAnimation: View {
@State private var isAnimating: Bool = false
@State private var arcAngle1: Double = 0
@State private var arcAngle2: Double = 180
@State private var circleHeight: CGFloat = 135
@State var color: Color
init(color: Color) {
self.color = color
}
var body: some View {
VStack {
ZStack {
Circle()
.fill(color)
.frame(height: circleHeight)
Circle()
.fill(Color.green).brightness(0.3)
.frame(height: isAnimating ? circleHeight / 2.5 : circleHeight / 1.8)
.animation(.easeInOut(duration: 1).repeatForever(autoreverses: true), value: UUID())
Group {
ArcShape(startAngle: .degrees(arcAngle1), endAngle: .degrees(arcAngle1 + 90))
.stroke(color, style: StrokeStyle(lineWidth: 5, lineCap: .round))
ArcShape(startAngle: .degrees(arcAngle2), endAngle: .degrees(arcAngle2 + 90))
.stroke(color, style: StrokeStyle(lineWidth: 5, lineCap: .round))
}
.frame(width: 190, height: 190)
.rotationEffect(.degrees(isAnimating ? 360 : 0))
.animation(.linear(duration: 2).repeatForever(autoreverses: false), value: UUID())
}
.onAppear {
isAnimating = true
}
}
}
}
struct ArcShape: Shape {
var startAngle: Angle
var endAngle: Angle
func path(in rect: CGRect) -> Path {
var path = Path()
path.addArc(center: CGPoint(x: rect.midX, y: rect.midY), radius: rect.width / 2,
startAngle: startAngle, endAngle: endAngle, clockwise: false)
return path
}
}
#Preview {
ArcRotationAnimation(color: .black)
}