Skip to content

Instantly share code, notes, and snippets.

View jarvis-wu's full-sized avatar
📱
writing code

Jarvis Zhaowei Wu jarvis-wu

📱
writing code
View GitHub Profile
struct Question: Codable {
var questionText: String
var weightedAnswer: Double
var selectedIndex: Int? = nil
var valuesYes: [ValueItem]
var valuesNo: [ValueItem]
var imageName: String
}
@jarvis-wu
jarvis-wu / question-object.swift
Created May 18, 2020 15:14
A question example
Question(questionText: questionTexts["qc4"]!,
weightedAnswer: 0,
valuesYes: [ValueItem(axis: "c0", value: 3)],
valuesNo: [ValueItem(axis: "c1", value: 3)],
imageName: "qc2-criminal")
@jarvis-wu
jarvis-wu / answerQuestion.swift
Created May 18, 2020 15:16
Answer a question
var axes = [String : (Double, Double)]();
for question in shuffled {
for valueYes in question.valuesYes {
if axes[valueYes.axis] == nil {
axes[valueYes.axis] = (0, 0)
}
if question.weightedAnswer > 0 {
axes[valueYes.axis]?.0 +=
question.weightedAnswer * Double(valueYes.value)
}
results["c"] = ResultValues(Int(((axes["c0"]!.0) / (axes["c0"]!.1) *
100).rounded()), Int(((axes["c1"]!.0) / (axes["c1"]!.1) *
100).rounded()))
@jarvis-wu
jarvis-wu / animation.swift
Created May 18, 2020 15:19
Card animation
let isMovingBack = currentQuestionNumber < oldValue
let animationDistance: CGFloat = (self.view.bounds.width +
self.questionCard.bounds.width) / 2 * (isMovingBack ? -1 : 1)
UIView.animate(withDuration: 0.4, delay: 0.1,
usingSpringWithDamping: 1, initialSpringVelocity: 0,
options: [.curveLinear], animations: {
self.questionCard.transform =
CGAffineTransform(translationX: -animationDistance, y: 0)
}) { (_) in
self.questionCard.transform =
let soundEffectRow = SettingsSwitchRowView(withTitle: "Sound effects",
isOn: (defaults.value(forKey: "soundEffectOn") as? Bool) ?? true,
didToggle: { toggle in
self.defaults.set(toggle.isOn, forKey: "soundEffectOn")
})
[ "t": ResultValues(l: 45, r: 14),
"j": ResultValues(l: 26, r: 50),
"m": ResultValues(l: 31, r: 38),
"e": ResultValues(l: 48, r: 31),
"c": ResultValues(l: 36, r: 38),
...... ]
[ ResultValues(l: 45, n: 41, r: 14),
ResultValues(l: 26, n: 24, r: 50),
ResultValues(l: 31, n: 31, r: 38),
ResultValues(l: 48, n: 21, r: 31),
ResultValues(l: 36, n: 26, r: 38),
...... ]
l = [45, 26, 31, 48, 36, ...]
n = [41, 24, 31, 21, 26, ...]
r = [14, 50, 38, 31, 38, ...]