swift
1import UIKit 2 3class ViewController: UIViewController { 4 5 var correctAnswer:Int? 6 var iv:UIImageView? 7 var questionView = UITextView() 8 var buttonList:[UIButton] = [] 9 10 var quizList:[Quiz] = Array(Quiz.defQuizzes) 11 12 13 var num = 0 14 var correctCount = 0 15 16 override func viewDidLoad() { 17 super.viewDidLoad() 18 // Do any additional setup after loading the view. 19 20 iv = setUpJudgmentImage() 21 22 questionView = setUpquestion(contents: quizList[num].question) 23 24 correctAnswer = quizList[num].answer 25 26 let c = setUpChoices(c1: quizList[num].choices[0], c2: quizList[num].choices[1], c3: quizList[num].choices[2]) 27 buttonList.append(c.0) 28 buttonList.append(c.1) 29 buttonList.append(c.2) 30 } 31 32 func setUpquestion(contents:String) -> UITextView{ 33 let question = UITextView() 34 question.isEditable = false 35 question.backgroundColor = .systemBlue 36 question.isSelectable = false 37 question.font = UIFont.systemFont(ofSize: 23) 38 question.textColor = .lightGray 39 question.text = contents 40 question.sizeToFit() 41 question.center = self.view.center 42 43 self.view.addSubview(question) 44 45 return question 46 } 47 48 func modQuestion(view: UITextView,contents:String){ 49 view.text = contents//位置が問題変わるごとに変わっていくバグ 50 view.sizeToFit() 51 } 52 53 func setUpChoices(c1:String,c2:String,c3:String) -> (UIButton,UIButton,UIButton){ 54 let button1 = UIButton() 55 button1.setTitle(c1, for: .normal) 56 button1.sizeToFit() 57 button1.setTitleColor(.systemBlue, for: .normal) 58 button1.center = CGPoint(x: self.view.bounds.width / 4, y: self.view.bounds.height * 3 / 4) 59 button1.tag = 0 60 button1.addTarget(self, action: #selector(buttonAction(_:)), for: .touchUpInside) 61 self.view.addSubview(button1) 62 63 let button2 = UIButton() 64 button2.setTitle(c2, for: .normal) 65 button2.sizeToFit() 66 button2.setTitleColor(.systemBlue, for: .normal) 67 button2.center = CGPoint(x: self.view.bounds.width / 2, y: self.view.bounds.height * 3 / 4) 68 button2.tag = 1 69 button2.addTarget(self, action: #selector(buttonAction(_:)), for: .touchUpInside) 70 self.view.addSubview(button2) 71 72 let button3 = UIButton() 73 button3.setTitle(c3, for: .normal) 74 button3.sizeToFit() 75 button3.setTitleColor(.systemBlue, for: .normal) 76 button3.center = CGPoint(x: self.view.bounds.width * 3 / 4, y: self.view.bounds.height * 3 / 4) 77 button3.tag = 2 78 button3.addTarget(self, action: #selector(buttonAction(_:)), for: .touchUpInside) 79 self.view.addSubview(button3) 80 81 return (button1,button2,button3) 82 } 83 84 func modButton(button:UIButton,str:String?){ 85 button.isEnabled = true//次のボタンでは押せるようにする 86 button.setTitle(str, for: .normal) 87 button.sizeToFit() 88 89 } 90 91 @objc func buttonAction(_ sender:UIButton){ 92 93 if let ansNum = correctAnswer{ 94 if sender.tag == ansNum{ 95 print("正解") 96 judgmentImage(bool: true) 97 correctCount += 1 98 }else{ 99 print("不正解") 100 judgmentImage(bool: false) 101 } 102 }else{ 103 print("エラー") 104 judgmentImage(bool: nil) 105 } 106 107 num += 1 108 //一回ボタンを押したら押したボタンと他のボタンを押せなくする処理 109 buttonList[0].isEnabled = false 110 buttonList[1].isEnabled = false 111 buttonList[2].isEnabled = false 112 113 114 DispatchQueue.main.asyncAfter(deadline: .now() + 5){ 115 if self.num == self.quizList.count { 116 self.modQuestion(view: self.questionView, contents: "あなたの正解数は") 117 self.iv?.isHidden = true 118 self.modButton(button: self.buttonList[0], str: nil) 119 self.modButton(button: self.buttonList[1], str: nil) 120 self.modButton(button: self.buttonList[2], str: nil) 121 122 let _ = self.correctCountView() 123 }else{ 124 self.iv?.isHidden = true 125 self.iv = self.setUpJudgmentImage() 126 self.modQuestion(view: self.questionView, contents: self.quizList[self.num].question) 127 128 self.correctAnswer = self.quizList[self.num].answer 129 130 self.modButton(button: self.buttonList[0], str: self.quizList[self.num].choices[0]) 131 self.modButton(button: self.buttonList[1], str: self.quizList[self.num].choices[1]) 132 self.modButton(button: self.buttonList[2], str: self.quizList[self.num].choices[2]) 133 } 134 135 } 136 } 137 138 func setUpJudgmentImage() -> UIImageView{ 139 let imageView = UIImageView(image: nil) 140 imageView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width / 2, height: self.view.bounds.width / 2) 141 imageView.center = CGPoint(x: self.view.bounds.width / 2, y: self.view.bounds.width / 2) 142 self.view.addSubview(imageView) 143 144 return imageView 145 } 146 147 func judgmentImage(bool:Bool?){ 148 if let b = bool{ 149 if b{ 150 iv?.image = UIImage(named: "mark_maru") 151 }else{ 152 iv?.image = UIImage(named: "mark_batsu") 153 } 154 }else{ 155 iv?.image = nil 156 } 157 } 158 159 func correctCountView() -> UITextView{ 160 let correctCountView = UITextView() 161 correctCountView.isEditable = false 162 correctCountView.isSelectable = false 163 correctCountView.textColor = .systemRed 164 correctCountView.font = UIFont.systemFont(ofSize: 60) 165 correctCountView.textAlignment = .center 166 correctCountView.text = String(correctCount) 167 correctCountView.sizeToFit() 168 correctCountView.center = CGPoint(x: self.view.bounds.width / 2, y: self.view.bounds.height * 2 / 3) 169 self.view.addSubview(correctCountView) 170 171 return correctCountView 172 } 173} 174
swift
1struct Quiz:Hashable { 2 var question:String 3 var choices:Array<String> 4 var answer:Int 5 var quizList:Set<Quiz> 6 7 static var defQuizzes:Set<Quiz>{ 8 get{ 9 var q = "富士山の標高は?" 10 var c = ["3776m","3190m","3193m"] 11 var a = 0 12 var quiz = Quiz(question: q, choices: c, answer: a) 13 quiz.add(quiz: quiz) 14 15 16 q = "渋沢栄一は何円札?" 17 c = ["一万円札","五千円札","千円札"] 18 a = 0 19 quiz.add(quiz: Quiz(question: q, choices: c, answer: a)) 20 21 q = "1+1 = ?" 22 c = ["1","2","3"] 23 a = 1 24 quiz.add(quiz: Quiz(question: q, choices: c, answer: a)) 25 26 q = "エベレストの標高は?" 27 c = ["7500m","8849m","8006m"] 28 a = 1 29 quiz.add(quiz: Quiz(question: q, choices: c, answer: a)) 30 31 q = "日本の人口は?" 32 c = ["1億2000万","8000万","5000万"] 33 a = 0 34 quiz.add(quiz: Quiz(question: q, choices: c, answer: a)) 35 36 return quiz.quizList 37 } 38 } 39 40 init(question:String,choices:Array<String>,answer:Int){ 41 self.question = question 42 self.choices = choices 43 self.answer = answer 44 quizList = [] 45 } 46 47 48 mutating func add(quiz:Quiz){ 49 quizList.insert(quiz) 50 } 51 52 53}
このようにViewのサイズが前のViewまでは一行であったのに何行にもなりサイズが変わってしまいます。
これはどうすれば直るでしょうか?
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。