目次
1. クイズ問題出題
2. ボタンが押された時のアクション
3.シュミレーターで確認
1. クイズ問題出題
- var questionNumber = 0
- var correctAnswerNumber = 0
- func question() {
-
- if questionNumber >= csvArray.count {
-
- let alert = UIAlertController(title: "漫画クイズ", message: "\(correctAnswerNumber)問正解", preferredStyle: .alert)
-
- let action = UIAlertAction(title: "終了", style: .default, handler: {
-
- (_) in self.dismiss(animated: true, completion: nil)
-
- })
-
- alert.addAction(action)
-
- present(alert, animated: true, completion: nil)
-
- return
-
- }
-
- let problemData = csvArray[questionNumber]
-
- label00.text = "第" + "\(questionNumber+1)問"
-
- label01.text = (problemData[0]as AnyObject as! String)
-
- let number = numberList()
-
- for i in 0...3 {
-
- button01[i].setTitle((problemData[number[i]]as AnyObject as! String), for: .normal)
-
- }
- }
-
- func numberList() -> [Int] {
-
- var list = [1,2,3,4]
-
- for _ in 1...10 {
-
- let i1 = Int(arc4random() % 4)
-
- let i2 = Int(arc4random() % 4)
-
- if i1 != i2 {
-
- list.swapAt(i1,i2)
-
- }
- }
-
- return list
-
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- csvToArray()
- question()
- }
viewDidLoad()内にquestion()
2. ボタンが押された時のアクション
- @IBAction func buttonCheck(_ sender: Any) {
-
- let answer = (sender as AnyObject).currentTitle!
-
- let questionData = csvArray[questionNumber]
-
- let answerNumber = questionData.index(of: answer!)
-
- if answerNumber == 1 {
-
- correctAnswerNumber += 1
-
- UIView.animate(withDuration: 1.0, animations: {
-
- UIApplication.shared.beginIgnoringInteractionEvents()
-
- self.currectAnswer.alpha = 1.0
-
- }) { (Bool) in
-
- self.currectAnswer.alpha = 0.0
-
- self.questionNumber += 1
-
- self.question()
-
- UIApplication.shared.endIgnoringInteractionEvents()
-
- }
-
- } else {
-
- UIView.animate(withDuration: 1.0, animations: {
-
- UIApplication.shared.beginIgnoringInteractionEvents()
-
- self.incorrectAnswer.alpha = 1.0
-
- }) { (Bool) in
-
- self.incorrectAnswer.alpha = 0.0
-
- self.questionNumber += 1
-
- self.question()
-
- UIApplication.shared.endIgnoringInteractionEvents()
-
- }
- }
- }
- }
3.シュミレーターで確認
シンプルなクイズアプリを作ってみました。
これから少しずつ、機能を追加していきたいと思います。
0 件のコメント:
コメントを投稿