SwiftUI_CardView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct ContentView: View { | |
@ObservedObject var viewModel = OrientationModel() | |
let cardView = CardView(card: Card.example) | |
var body: some View { | |
ScrollView(.horizontal) { | |
HStack(spacing: 0) { | |
self.cardView | |
.font(.title) | |
.frame(width: 350, height: 350) | |
}.padding(30) | |
} | |
} | |
} | |
struct CardView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} | |
struct CardView: View { | |
var card: Card | |
@ObservedObject var viewModel = OrientationModel() | |
var body: some View { | |
ZStack { | |
RoundedRectangle(cornerRadius: 25, style: .continuous) | |
.fill(viewModel.backColor) | |
.cornerRadius(50) | |
VStack { | |
Text(card.prompt) | |
.foregroundColor(viewModel.promptColor) | |
.font(Font.system(size: 20.0)) | |
.padding(.bottom, 30) | |
Text(viewModel.answer == "" ? card.answer : viewModel.answer) | |
.foregroundColor(viewModel.answerColor) | |
.font(Font.system(size: 20.0)) | |
} | |
.multilineTextAlignment(.center) | |
} | |
.frame(width: 300, height: 300) | |
.shadow(radius: 5) | |
.onTapGesture { | |
var s = self | |
self.viewModel.answer == "" ? s.onCellEvent(.cellEvent(st: "YES")): s.onCellEvent(.cellEvent(st: "")) | |
} | |
} | |
mutating func onCellEvent(_ event: CellEvent<String>) { | |
switch event { | |
case let .cellEvent(st): | |
if st == "" { | |
viewModel.answer = "" | |
viewModel.promptColor = Color.black | |
viewModel.answerColor = Color.gray | |
viewModel.backColor = Color.white | |
} else { | |
viewModel.answer = st | |
viewModel.promptColor = Color.red | |
viewModel.answerColor = Color.yellow | |
viewModel.backColor = Color.green | |
} | |
} | |
} | |
} | |
struct Card { | |
let prompt: String | |
let answer: String | |
static var example: Card { | |
return Card(prompt: "Do you like programming", answer: "Please tap") | |
} | |
} | |
final class OrientationModel: ObservableObject { | |
@Published var answer = String() | |
@Published var backColor = Color.white | |
@Published var promptColor = Color.black | |
@Published var answerColor = Color.gray | |
} | |
public enum CellEvent<String> { | |
case cellEvent(st: String) | |
} |
Author
DaisukeNagata
commented
Dec 22, 2019
The message has been changed.
Card(prompt: "Do you like programming", answer: "Please tap")
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment