Skip to content

Instantly share code, notes, and snippets.

@deurell
Created July 25, 2023 14:48
Show Gist options
  • Save deurell/151a9a0f4e9133c33ddfd1c7cf8daf8e to your computer and use it in GitHub Desktop.
Save deurell/151a9a0f4e9133c33ddfd1c7cf8daf8e to your computer and use it in GitHub Desktop.
import SwiftUI
struct GameMetricsView: View {
@Environment(\.colorScheme) var colorScheme
var gameMode: String
var totalPlays: Int
@Binding var totalAverage: Double
@State private var animatedTotalAverage: Double = 0.0
var body: some View {
VStack(spacing: 8) {
Text(gameMode)
.font(.caption2)
.foregroundColor(.blue)
VStack(spacing: 4) {
ZStack {
Circle()
.stroke(Color.gray, style: StrokeStyle(lineWidth: 10.0, lineCap: .round, lineJoin: .round))
.frame(width: 60, height: 60)
Circle()
.trim(from: 0, to: CGFloat(animatedTotalAverage))
.stroke(totalAverage >= 0.9 ? Color.green : Color.blue, style: StrokeStyle(lineWidth: 10.0, lineCap: .round, lineJoin: .round))
.frame(width: 60, height: 60)
.rotationEffect(.degrees(-90))
Text("\(totalPlays)")
.font(.title)
.foregroundColor(colorScheme == .dark ? Color.white : Color.black)
.padding(4)
}
.onAppear {
animatedTotalAverage = totalAverage
}
}
}
.padding(.horizontal, 20)
.padding(.vertical, 10)
.background(
RoundedRectangle(cornerRadius: 16)
.fill(colorScheme == .dark ? Color.black : Color.white)
.shadow(color: .black.opacity(0.2), radius: 4, x: 0, y: 4)
)
.onChange(of: totalAverage) { newValue in
withAnimation {
animatedTotalAverage = newValue
}
}
}
}
struct GameMetricsView_Previews: PreviewProvider {
@State static var totalAverage: Double = 0.5
static var previews: some View {
GameMetricsView(gameMode: "Game Mode", totalPlays: 50, totalAverage: $totalAverage)
.previewLayout(.sizeThatFits)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment