TokiTrainer/Toki Trainer/Views/FlashCardView.swift

77 lines
2.2 KiB
Swift
Raw Normal View History

2021-11-05 22:20:48 +00:00
//
// FlashCardView.swift
// Toki Trainer
//
// Created by Avery Ada Pace on 11/5/21.
//
import SwiftUI
struct FlashCardView: View {
let screen = UIScreen.main.bounds
2021-11-06 00:23:33 +00:00
@State var isFaceDown = false
@State var rotationAngle: Double = 0
2021-11-05 22:20:48 +00:00
var body: some View {
2021-11-06 00:23:33 +00:00
let flipDegrees = isFaceDown ? 0.0 : 180.0
2021-11-05 22:20:48 +00:00
2021-11-06 00:23:33 +00:00
Text("sina")
.modifier(CardFlipModifier(isFaceDown: isFaceDown, frontText: "linja", backText: "long, very thin, floppy thing, e.g. string, rope, hair, thread, cord, chain"))
.frame(width: 0.8 * screen.width, height: 200.0)
.font(.title)
.shadow(color: .gray, radius: 10.0, x: 10, y: 10)
.rotation3DEffect(self.isFaceDown ? Angle(degrees: 180) : Angle(degrees: 0), axis: (x: 0.0, y: 10.0, z: 0.0))
.animation(.default)
.onTapGesture {
self.isFaceDown.toggle()
}
2021-11-05 22:20:48 +00:00
}
}
struct CardFlipModifier: AnimatableModifier {
2021-11-06 00:23:33 +00:00
var frontText: String
var backText: String
var isFaceDown: Bool
2021-11-05 22:20:48 +00:00
var rotationAngle: Double
2021-11-06 00:23:33 +00:00
init(isFaceDown: Bool, frontText: String, backText: String) {
rotationAngle = isFaceDown ? 180 : 0
self.isFaceDown = isFaceDown
self.frontText = frontText
self.backText = backText
2021-11-05 22:20:48 +00:00
}
var animatableData: Double {
get { rotationAngle }
set { rotationAngle = newValue }
}
func body(content: Content) -> some View {
2021-11-06 00:23:33 +00:00
return ZStack {
2021-11-05 22:20:48 +00:00
RoundedRectangle(cornerRadius: 20.0)
2021-11-06 00:23:33 +00:00
.fill(rotationAngle < 90 ? Color.blue : Color.cyan)
.animation(.none)
Text(frontText)
.font(.title)
2021-11-05 22:20:48 +00:00
.opacity(rotationAngle < 90 ? 1.0 : 0.0)
2021-11-06 00:23:33 +00:00
.animation(.none)
Text(backText)
.font(.subheadline)
.padding()
.opacity(rotationAngle < 90 ? 0.0 : 1.0)
.scaleEffect(CGSize(width: -1.0, height: 1.0))
.animation(.none)
2021-11-05 22:20:48 +00:00
}
}
}
struct FlashCardView_Previews: PreviewProvider {
static var previews: some View {
FlashCardView()
}
}