iOS开发Swift-7-得分,问题序号,约束对象,提示框,类方法与静态方法-趣味问答App
1.根据用户回答计算得分
ViewController.swift:
import UIKit class ViewController: UIViewController { var questionIndex = 0 var score = 0 @IBOutlet weak var questionLabel: UILabel! @IBOutlet weak var scoreLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. questionLabel.text = queastions[0].text } @IBAction func answerPressed(_ sender: UIButton) { checkAnswer(sender.tag) questionIndex += 1 nextQuestion() } func nextQuestion(){ if questionIndex <= 12{ questionLabel.text = queastions[questionIndex].text }else{ questionIndex = 0 let alert = UIAlertController(title: "漂亮!", message: "您已经完成了所有问题,要再来一遍吗?", preferredStyle: .alert) let action = UIAlertAction(title: "再来一遍", style: .default, handler: { _ in self.questionLabel.text = queastions[0].text }) alert.addAction(action) // present(alert, animated: true) } } func checkAnswer(_ tag: Int){ if tag == 1 { if queastions[questionIndex].answer == true{ print("huidazhengque") score += 1 scoreLabel.text = "总得分:\(score)" }else{ print("huidacuowu") } }else{ if queastions[questionIndex].answer == true{ print("huidacuowu") }else{ print("huidazhengque") score += 1 scoreLabel.text = "总得分:\(score)" } } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
2.显示题目序号
ViewController.swift:
import UIKit class ViewController: UIViewController { var questionIndex = 0 var score = 0 @IBOutlet weak var questionLabel: UILabel! @IBOutlet weak var scoreLabel: UILabel! @IBOutlet weak var progressLable: UILabel! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. questionLabel.text = queastions[0].text } @IBAction func answerPressed(_ sender: UIButton) { checkAnswer(sender.tag) questionIndex += 1 nextQuestion() progressLable.text = "\(questionIndex + 1) / 13" } func nextQuestion(){ if questionIndex <= 12{ questionLabel.text = queastions[questionIndex].text }else{ questionIndex = 0 let alert = UIAlertController(title: "漂亮!", message: "您已经完成了所有问题,要再来一遍吗?", preferredStyle: .alert) let action = UIAlertAction(title: "再来一遍", style: .default, handler: { _ in self.questionLabel.text = queastions[0].text }) alert.addAction(action) // present(alert, animated: true) } } func checkAnswer(_ tag: Int){ if tag == 1 { if queastions[questionIndex].answer == true{ print("huidazhengque") score += 1 scoreLabel.text = "总得分:\(score)" }else{ print("huidacuowu") } }else{ if queastions[questionIndex].answer == true{ print("huidacuowu") }else{ print("huidazhengque") score += 1 scoreLabel.text = "总得分:\(score)" } } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
3.为屏幕进度条更改约束
将1:13的宽度约束拖入ViewController。
因为progressBarView是只读,所以要根据屏幕宽度计算出1/13的宽度,然后加到Constant中。
ViewController.swift:
import UIKit class ViewController: UIViewController { var questionIndex = 0 var score = 0 @IBOutlet weak var questionLabel: UILabel! @IBOutlet weak var scoreLabel: UILabel! @IBOutlet weak var progressLable: UILabel! @IBOutlet weak var progressBarViewWidth: NSLayoutConstraint! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. questionLabel.text = queastions[0].text } @IBAction func answerPressed(_ sender: UIButton) { checkAnswer(sender.tag) questionIndex += 1 nextQuestion() progressLable.text = "\(questionIndex + 1) / 13" progressBarViewWidth.constant = (view.frame.width / 13) * CGFloat(questionIndex) } func nextQuestion(){ if questionIndex <= 12{ questionLabel.text = queastions[questionIndex].text }else{ questionIndex = 0 let alert = UIAlertController(title: "漂亮!", message: "您已经完成了所有问题,要再来一遍吗?", preferredStyle: .alert) let action = UIAlertAction(title: "再来一遍", style: .default, handler: { _ in self.questionLabel.text = queastions[0].text self.scoreLabel.text = "总得分:0" }) alert.addAction(action) // present(alert, animated: true) } } func checkAnswer(_ tag: Int){ if tag == 1 { if queastions[questionIndex].answer { print("huidazhengque") score += 1 scoreLabel.text = "总得分:\(score)" }else{ print("huidacuowu") } }else{ if queastions[questionIndex].answer { print("huidacuowu") }else{ print("huidazhengque") score += 1 scoreLabel.text = "总得分:\(score)" } } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
4.制作弹窗
https://github.com/relatedcode/ProgressHUD
将gitHub上拉的swift文件拖到项目中去。
在ViewController中调用这个swift中的方法。
import UIKit class ViewController: UIViewController { var questionIndex = 0 var score = 0 @IBOutlet weak var questionLabel: UILabel! @IBOutlet weak var scoreLabel: UILabel! @IBOutlet weak var progressLable: UILabel! @IBOutlet weak var progressBarViewWidth: NSLayoutConstraint! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. questionLabel.text = queastions[0].text } @IBAction func answerPressed(_ sender: UIButton) { checkAnswer(sender.tag) questionIndex += 1 nextQuestion() progressLable.text = "\(questionIndex + 1) / 13" progressBarViewWidth.constant = (view.frame.width / 13) * CGFloat(questionIndex) } func nextQuestion(){ if questionIndex <= 12{ questionLabel.text = queastions[questionIndex].text }else{ questionIndex = 0 let alert = UIAlertController(title: "漂亮!", message: "您已经完成了所有问题,要再来一遍吗?", preferredStyle: .alert) let action = UIAlertAction(title: "再来一遍", style: .default, handler: { _ in self.questionLabel.text = queastions[0].text self.scoreLabel.text = "总得分:0" }) alert.addAction(action) // present(alert, animated: true) } } func checkAnswer(_ tag: Int){ if tag == 1 { if queastions[questionIndex].answer { ProgressHUD.showSucceed("答对了") score += 1 scoreLabel.text = "总得分:\(score)" }else{ ProgressHUD.showError("答错了") } }else{ if queastions[questionIndex].answer { ProgressHUD.showError("答错了") }else{ ProgressHUD.showSucceed("答对了") score += 1 scoreLabel.text = "总得分:\(score)" } } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
5.启动测试
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性