Skip to content

Commit 4f908d7

Browse files
committed
Add animation
1 parent 9ed2bc1 commit 4f908d7

File tree

4 files changed

+140
-11
lines changed

4 files changed

+140
-11
lines changed

RestartApplication/Screens/HomeView.swift

+19-2
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,23 @@ import SwiftUI
99

1010
struct HomeView: View {
1111
@AppStorage("onboarding") var isOnBoardingViewActive: Bool = false
12+
@State private var isAnimating: Bool = false
1213
var body: some View {
1314
VStack {
14-
15+
Spacer()
1516
//header
1617
ZStack {
1718
CircleGroupView(ShapeColor: .gray, ShapeOpacity: 0.1)
1819
Image("character-2")
1920
.resizable()
2021
.scaledToFit()
22+
2123
.padding()
24+
.offset(y: isAnimating ? 35 : -35)
25+
.animation(
26+
Animation.easeInOut(duration: 4)
27+
.repeatForever(),
28+
value: isAnimating)
2229
}
2330

2431
//center
@@ -35,10 +42,15 @@ struct HomeView: View {
3542
Spacer()
3643

3744
Button(action: {
38-
isOnBoardingViewActive = true
45+
withAnimation {
46+
playSound(sound: "success", type: "m4a")
47+
isOnBoardingViewActive = true
48+
}
49+
3950
}, label: {
4051
Image(systemName: "arrow.triangle.2.circlepath.circle.fill")
4152
.imageScale(.large)
53+
4254

4355
Text("Restart")
4456
.font(.system(.title3 , design: .rounded))
@@ -51,6 +63,11 @@ struct HomeView: View {
5163

5264

5365
}
66+
.onAppear(perform: {
67+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.6, execute: {
68+
isAnimating = true
69+
})
70+
})
5471
}
5572
}
5673

RestartApplication/Screens/OnBoardingView.swift

+91-9
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,29 @@ import SwiftUI
99

1010
struct OnBoardingView: View {
1111
@AppStorage("onboarding") var isOnBoardingActive: Bool = true
12+
@State private var buttonWidth: Double = UIScreen.main.bounds.width - 80
13+
@State private var buttonOffset: CGFloat = 0
14+
@State private var imageOffset: CGSize = .zero
15+
@State private var indicateorIpacity: Double = 1.0
16+
@State private var textTitle: String = "Share."
17+
18+
let hapticFeedback = UINotificationFeedbackGenerator()
19+
20+
@State private var inAnimating: Bool = false
21+
1222
var body: some View {
1323
ZStack {
1424
Color("ColorBlue").ignoresSafeArea()
1525
VStack(spacing: 20) {
16-
// Header
26+
// MARK: - Header
1727
Spacer()
1828
VStack {
19-
Text("Share.")
29+
Text(textTitle)
2030
.font(.system(size: 60))
2131
.fontWeight(.heavy)
2232
.foregroundColor(.white)
33+
.transition(.opacity)
34+
.id(textTitle)
2335
Text("""
2436
its not showing much we give but
2537
how much i giove you put into your
@@ -32,15 +44,53 @@ how much i giove you put into your
3244

3345

3446
}//end of vstack
35-
//Cente
47+
.opacity(inAnimating ? 1 : 0)
48+
.offset(y: inAnimating ? 0 : -40)
49+
.animation(.easeOut(duration: 1) , value: inAnimating)
50+
// MARK: - Center
3651
ZStack {
3752
CircleGroupView(ShapeColor: .white, ShapeOpacity: 0.2)
53+
.offset(x: imageOffset.width * -1)
54+
.blur(radius: abs(imageOffset.width / 5))
55+
.animation(.easeOut(duration: 1), value: imageOffset)
56+
3857
Image("character-1")
3958
.resizable()
4059
.scaledToFit()
60+
.opacity(inAnimating ? 1 : 0)
61+
.animation(.easeOut, value: inAnimating)
62+
.offset(x: imageOffset.width * 1.2 , y: 0)
63+
.rotationEffect(.degrees(Double(imageOffset.width / 20 )))
64+
.gesture(DragGesture().onChanged{ gestuer in
65+
if abs(imageOffset.width) <= 150 {
66+
imageOffset = gestuer.translation
67+
withAnimation(.linear(duration: 0.25)) {
68+
indicateorIpacity = 0
69+
textTitle = "Give."
70+
}
71+
}
72+
73+
}.onEnded{ _ in
74+
imageOffset = .zero
75+
withAnimation(.linear(duration: 0.25)) {
76+
indicateorIpacity = 1
77+
textTitle = "Share."
78+
}
79+
80+
})
81+
.animation(.easeOut(duration: 1),value: imageOffset)
4182
}//end of ztack
83+
.overlay(Image(systemName: "arrow.left.and.right.circle")
84+
.font(.system(size: 44, weight: .ultraLight))
85+
.foregroundColor(.white)
86+
.offset(y:20)
87+
.opacity(inAnimating ? 1 : 0)
88+
.animation(.easeOut(duration: 1).delay(2), value: inAnimating)
89+
.opacity(indicateorIpacity)
90+
, alignment: .bottom
91+
)
4292
Spacer()
43-
//Footer
93+
// MARK: - Footer
4494

4595
ZStack {
4696
//part 1
@@ -60,7 +110,7 @@ how much i giove you put into your
60110
HStack {
61111
Capsule()
62112
.fill(Color.red.opacity(0.8))
63-
.frame(width: 80)
113+
.frame(width:buttonOffset + 80)
64114
Spacer()
65115
}
66116
// part 4
@@ -77,19 +127,51 @@ how much i giove you put into your
77127
}
78128
.foregroundColor(.white)
79129
.frame(height: 80 , alignment: .center)
80-
.onTapGesture {
81-
isOnBoardingActive = false
82-
}
130+
.offset(x: buttonOffset)
131+
.gesture(
132+
DragGesture()
133+
.onChanged { gesture in
134+
if gesture.translation.width > 0 && buttonOffset <= buttonWidth - 80{
135+
buttonOffset = gesture.translation.width
136+
}
137+
138+
}
139+
.onEnded { _ in
140+
141+
withAnimation(.easeOut(duration: 0.4)) {
142+
if buttonOffset > buttonWidth / 2 {
143+
hapticFeedback.notificationOccurred(.success)
144+
playSound(sound: "chimeup", type: "mp3")
145+
buttonOffset = buttonWidth - 80
146+
isOnBoardingActive = false
147+
}else
148+
{
149+
hapticFeedback.notificationOccurred(.warning)
150+
buttonOffset = 0
151+
}
152+
}
153+
154+
155+
}
156+
)
83157

84158
Spacer()
85159
}
86160

87161

88162
}//end od footer
89-
.frame(height: 80 , alignment: .center)
163+
.frame(width: buttonWidth,height: 80 , alignment: .center)
90164
.padding()
165+
.opacity(inAnimating ? 1 : 0)
166+
.offset(y: inAnimating ? 0 : 40)
167+
.animation(.easeIn(duration: 1) , value: inAnimating)
91168
}//:E VStack
92169
}//:E ZStack
170+
.onAppear(perform: {
171+
inAnimating = true
172+
})
173+
.preferredColorScheme(.dark)
174+
93175
}
94176
}
95177

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// AudioPlayer.swift
3+
// RestartApplication
4+
//
5+
// Created by Reza on 10/7/2024.
6+
//
7+
8+
import Foundation
9+
import AVFoundation
10+
11+
var audioPlayer: AVAudioPlayer?
12+
func playSound(sound : String , type: String) {
13+
if let path = Bundle.main.path(forResource: sound, ofType: type) {
14+
do {
15+
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
16+
audioPlayer?.play()
17+
} catch {
18+
print("Could not play the sound")
19+
}
20+
}
21+
}

RestartApplication/Views/CircleGroupView.swift

+9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ struct CircleGroupView: View {
1111

1212
@State var ShapeColor : Color
1313
@State var ShapeOpacity : Double
14+
@State private var isAnimating: Bool = false
1415

1516
var body: some View {
1617
ZStack {
@@ -21,6 +22,14 @@ struct CircleGroupView: View {
2122
.stroke(ShapeColor.opacity(ShapeOpacity) , lineWidth: 80)
2223
.frame(width: 260 , height: 260)
2324
}
25+
.blur(radius: isAnimating ? 0 : 10)
26+
.opacity(isAnimating ? 1 : 0)
27+
.scaleEffect(isAnimating ? 1 : 0.5)
28+
.animation(.easeOut(duration: 1) , value: isAnimating)
29+
.onAppear(perform: {
30+
isAnimating = true
31+
})
32+
2433
}
2534
}
2635

0 commit comments

Comments
 (0)