From 6a81806abfb22f82fc5efa97bccc7850b849efe2 Mon Sep 17 00:00:00 2001 From: Avery Pace Date: Fri, 5 Nov 2021 20:23:33 -0400 Subject: [PATCH] Flash card flip animation --- .../xcdebugger/Breakpoints_v2.xcbkptlist | 18 ------ Toki Trainer/Views/FlashCardView.swift | 60 +++++++++++-------- 2 files changed, 35 insertions(+), 43 deletions(-) diff --git a/Toki Trainer.xcodeproj/xcuserdata/averyadapace.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/Toki Trainer.xcodeproj/xcuserdata/averyadapace.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index a6ca121..cf492b1 100644 --- a/Toki Trainer.xcodeproj/xcuserdata/averyadapace.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/Toki Trainer.xcodeproj/xcuserdata/averyadapace.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -3,22 +3,4 @@ uuid = "2238B32A-BF80-4351-B121-F5E36A8C39FC" type = "1" version = "2.0"> - - - - - - diff --git a/Toki Trainer/Views/FlashCardView.swift b/Toki Trainer/Views/FlashCardView.swift index 1f63527..0f08fc3 100644 --- a/Toki Trainer/Views/FlashCardView.swift +++ b/Toki Trainer/Views/FlashCardView.swift @@ -10,38 +10,39 @@ import SwiftUI struct FlashCardView: View { let screen = UIScreen.main.bounds - @State var flipped = false + @State var isFaceDown = false + @State var rotationAngle: Double = 0 var body: some View { - let flipDegrees = flipped ? 0.0 : 180.0 - - ZStack() { - Text("sina").opacity(flipped ? 0.0 : 1.0) - Text("you") - .opacity(flipped ? 1.0 : 0.0) - .rotation3DEffect(Angle(degrees: -180 + flipDegrees), axis: (x: 0.0, y: 10.0, z: 0.0)) - } - .frame(width: 0.8 * screen.width, height: 200.0) - .font(.title) - .background(self.flipped ? .cyan : .mint) - .cornerRadius(20) - .shadow(color: .gray, radius: 10.0, x: 10, y: 10) - .rotation3DEffect(self.flipped ? Angle(degrees: 180) : Angle(degrees: 0), axis: (x: 0.0, y: 10.0, z: 0.0) - ) - .animation(.default) - .onTapGesture { - self.flipped.toggle() - } + let flipDegrees = isFaceDown ? 0.0 : 180.0 + 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() + } + } } struct CardFlipModifier: AnimatableModifier { + + var frontText: String + var backText: String + var isFaceDown: Bool var rotationAngle: Double - init(isFaceUp: Bool) { - rotationAngle = isFaceUp ? 0 : 180 + init(isFaceDown: Bool, frontText: String, backText: String) { + rotationAngle = isFaceDown ? 180 : 0 + self.isFaceDown = isFaceDown + self.frontText = frontText + self.backText = backText } var animatableData: Double { @@ -50,11 +51,20 @@ struct CardFlipModifier: AnimatableModifier { } func body(content: Content) -> some View { - ZStack { + return ZStack { RoundedRectangle(cornerRadius: 20.0) - .fill(Color.blue.opacity(rotationAngle < 90 ? 0.5 : 1.0)) - content + .fill(rotationAngle < 90 ? Color.blue : Color.cyan) + .animation(.none) + Text(frontText) + .font(.title) .opacity(rotationAngle < 90 ? 1.0 : 0.0) + .animation(.none) + Text(backText) + .font(.subheadline) + .padding() + .opacity(rotationAngle < 90 ? 0.0 : 1.0) + .scaleEffect(CGSize(width: -1.0, height: 1.0)) + .animation(.none) } } }