Skip to content

Commit c68db9e

Browse files
committed
Fix HUD animations
- Rename moreUI to HUD
1 parent 5f6bddf commit c68db9e

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

QuickStart/iOS/Game States/5 - TitleState/5C - TitleUI.swift

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,25 @@ struct TitleUI: View {
2828
static var suppressAppearanceAnimation = false
2929

3030
/// This flag controls the display of superficial UI for demonstration.
31-
@State private var showMoreUI = showMoreUIGlobal {
31+
@State private var showHUD = TitleUI.showHUDGlobal {
3232
didSet {
33-
TitleUI.showMoreUIGlobal = showMoreUI
33+
TitleUI.showHUDGlobal = showHUD
3434
}
3535
}
3636

37-
/// Since TitleUI is also used in PlayUI, we copy the showMoreUI setting to a static variable which persists across multiple game states, otherwise it would get reset when the game state changes.
38-
static var showMoreUIGlobal = false
37+
/// Since TitleUI is also used in PlayUI, we copy the showHUD setting to a static variable which persists across multiple game states, otherwise it would get reset when the game state changes.
38+
static var showHUDGlobal = false
3939

40+
/// This flag is set to `false` after the HUD is shown, to prevent it from reanimating when TitleUI is shown inside PlayUI after the game state changes.
41+
@State private var animateHUDAppearance = true
42+
4043
var body: some View {
4144

4245
VStack {
4346

4447
Spacer()
4548

46-
moreUI
49+
hudUI
4750

4851
Spacer()
4952

@@ -62,8 +65,8 @@ struct TitleUI: View {
6265
}
6366
}
6467
.onAppear {
65-
// Set a flag to start the initial animation.
66-
self.didAppear = true
68+
self.didAppear = true // Set a flag to start the initial animation.
69+
self.showHUD = TitleUI.showHUDGlobal // Sync the instance and global flags.
6770
}
6871

6972
}
@@ -77,7 +80,7 @@ struct TitleUI: View {
7780

7881
VStack {
7982

80-
toggleMoreUIButton
83+
toggleHUDButton
8184

8285
nextStateButton
8386

@@ -99,8 +102,8 @@ struct TitleUI: View {
99102
}
100103
}
101104

102-
var toggleMoreUIButton: some View {
103-
Button(action: toggleMoreUI) {
105+
var toggleHUDButton: some View {
106+
Button(action: toggleHUD) {
104107
QuickStartButtonLabel(text: "TOGGLE MORE UI", color: .purple)
105108
}
106109
}
@@ -114,32 +117,31 @@ struct TitleUI: View {
114117
}
115118
}
116119

117-
func toggleMoreUI() {
118-
showMoreUI.toggle()
119-
TitleUI.showMoreUIGlobal = showMoreUI
120+
func toggleHUD() {
121+
showHUD.toggle()
120122
}
121123

122124
// MARK: - Superficial UI
123125
// To demonstrate animation and HUD overlays.
124126

125-
var moreUI: some View {
127+
var hudUI: some View {
126128

127129
HStack(alignment: .center) {
128130

129-
if showMoreUI || TitleUI.showMoreUIGlobal {
131+
if showHUD || TitleUI.showHUDGlobal {
130132

131-
moreUIButtonStack
133+
hudButtonStack
132134
.transition(.move(edge: .leading))
133135

134136
Spacer()
135137

136-
moreUIButtonStack
138+
hudButtonStack
137139
.transition(.move(edge: .trailing))
138140
}
139141
}
140142
}
141143

142-
var moreUIButtonStack: some View {
144+
var hudButtonStack: some View {
143145

144146
VStack(alignment: .leading, spacing: 50) {
145147

@@ -163,8 +165,15 @@ struct TitleUI: View {
163165
.foregroundColor(.randomExcludingBlackWhite)
164166
.opacity(0.85)
165167
.shadow(color: .black, radius: 5, x: 0, y: 10)
166-
.animation(.spring(response: 0.4, dampingFraction: 0.5, blendDuration: 0.5))
168+
.animation(self.animateHUDAppearance ? .spring(response: 0.4, dampingFraction: 0.5, blendDuration: 0.5) : .none)
167169
.overlay(Image(systemName: randomImageName).font(.largeTitle).foregroundColor(.white))
170+
.onAppear {
171+
/// Prevent re-animation on a new game state.
172+
self.animateHUDAppearance = false
173+
}
174+
.onDisappear {
175+
self.animateHUDAppearance = true
176+
}
168177
}
169178

170179
}

0 commit comments

Comments
 (0)