• iOS开发Swift-7-得分,问题序号,约束对象,提示框,类方法与静态方法-趣味问答App


    1.根据用户回答计算得分

     ViewController.swift:

    1. import UIKit
    2. class ViewController: UIViewController {
    3. var questionIndex = 0
    4. var score = 0
    5. @IBOutlet weak var questionLabel: UILabel!
    6. @IBOutlet weak var scoreLabel: UILabel!
    7. override func viewDidLoad() {
    8. super.viewDidLoad()
    9. // Do any additional setup after loading the view, typically from a nib.
    10. questionLabel.text = queastions[0].text
    11. }
    12. @IBAction func answerPressed(_ sender: UIButton) {
    13. checkAnswer(sender.tag)
    14. questionIndex += 1
    15. nextQuestion()
    16. }
    17. func nextQuestion(){
    18. if questionIndex <= 12{
    19. questionLabel.text = queastions[questionIndex].text
    20. }else{
    21. questionIndex = 0
    22. let alert = UIAlertController(title: "漂亮!", message: "您已经完成了所有问题,要再来一遍吗?", preferredStyle: .alert)
    23. let action = UIAlertAction(title: "再来一遍", style: .default, handler: { _ in
    24. self.questionLabel.text = queastions[0].text
    25. })
    26. alert.addAction(action)
    27. //
    28. present(alert, animated: true)
    29. }
    30. }
    31. func checkAnswer(_ tag: Int){
    32. if tag == 1 {
    33. if queastions[questionIndex].answer == true{
    34. print("huidazhengque")
    35. score += 1
    36. scoreLabel.text = "总得分:\(score)"
    37. }else{
    38. print("huidacuowu")
    39. }
    40. }else{
    41. if queastions[questionIndex].answer == true{
    42. print("huidacuowu")
    43. }else{
    44. print("huidazhengque")
    45. score += 1
    46. scoreLabel.text = "总得分:\(score)" }
    47. } }
    48. override func didReceiveMemoryWarning() {
    49. super.didReceiveMemoryWarning()
    50. // Dispose of any resources that can be recreated.
    51. }
    52. }

    2.显示题目序号

     ViewController.swift:

    1. import UIKit
    2. class ViewController: UIViewController {
    3. var questionIndex = 0
    4. var score = 0
    5. @IBOutlet weak var questionLabel: UILabel!
    6. @IBOutlet weak var scoreLabel: UILabel!
    7. @IBOutlet weak var progressLable: UILabel!
    8. override func viewDidLoad() {
    9. super.viewDidLoad()
    10. // Do any additional setup after loading the view, typically from a nib.
    11. questionLabel.text = queastions[0].text
    12. }
    13. @IBAction func answerPressed(_ sender: UIButton) {
    14. checkAnswer(sender.tag)
    15. questionIndex += 1
    16. nextQuestion()
    17. progressLable.text = "\(questionIndex + 1) / 13"
    18. }
    19. func nextQuestion(){
    20. if questionIndex <= 12{
    21. questionLabel.text = queastions[questionIndex].text
    22. }else{
    23. questionIndex = 0
    24. let alert = UIAlertController(title: "漂亮!", message: "您已经完成了所有问题,要再来一遍吗?", preferredStyle: .alert)
    25. let action = UIAlertAction(title: "再来一遍", style: .default, handler: { _ in
    26. self.questionLabel.text = queastions[0].text
    27. })
    28. alert.addAction(action)
    29. //
    30. present(alert, animated: true)
    31. }
    32. }
    33. func checkAnswer(_ tag: Int){
    34. if tag == 1 {
    35. if queastions[questionIndex].answer == true{
    36. print("huidazhengque")
    37. score += 1
    38. scoreLabel.text = "总得分:\(score)"
    39. }else{
    40. print("huidacuowu")
    41. }
    42. }else{
    43. if queastions[questionIndex].answer == true{
    44. print("huidacuowu")
    45. }else{
    46. print("huidazhengque")
    47. score += 1
    48. scoreLabel.text = "总得分:\(score)" }
    49. } }
    50. override func didReceiveMemoryWarning() {
    51. super.didReceiveMemoryWarning()
    52. // Dispose of any resources that can be recreated.
    53. }
    54. }

    3.为屏幕进度条更改约束

    将1:13的宽度约束拖入ViewController。

     因为progressBarView是只读,所以要根据屏幕宽度计算出1/13的宽度,然后加到Constant中。

     ViewController.swift:

    1. import UIKit
    2. class ViewController: UIViewController {
    3. var questionIndex = 0
    4. var score = 0
    5. @IBOutlet weak var questionLabel: UILabel!
    6. @IBOutlet weak var scoreLabel: UILabel!
    7. @IBOutlet weak var progressLable: UILabel!
    8. @IBOutlet weak var progressBarViewWidth: NSLayoutConstraint!
    9. override func viewDidLoad() {
    10. super.viewDidLoad()
    11. // Do any additional setup after loading the view, typically from a nib.
    12. questionLabel.text = queastions[0].text
    13. }
    14. @IBAction func answerPressed(_ sender: UIButton) {
    15. checkAnswer(sender.tag)
    16. questionIndex += 1
    17. nextQuestion()
    18. progressLable.text = "\(questionIndex + 1) / 13"
    19. progressBarViewWidth.constant = (view.frame.width / 13) * CGFloat(questionIndex)
    20. }
    21. func nextQuestion(){
    22. if questionIndex <= 12{
    23. questionLabel.text = queastions[questionIndex].text
    24. }else{
    25. questionIndex = 0
    26. let alert = UIAlertController(title: "漂亮!", message: "您已经完成了所有问题,要再来一遍吗?", preferredStyle: .alert)
    27. let action = UIAlertAction(title: "再来一遍", style: .default, handler: { _ in
    28. self.questionLabel.text = queastions[0].text
    29. self.scoreLabel.text = "总得分:0"
    30. })
    31. alert.addAction(action)
    32. //
    33. present(alert, animated: true)
    34. }
    35. }
    36. func checkAnswer(_ tag: Int){
    37. if tag == 1 {
    38. if queastions[questionIndex].answer {
    39. print("huidazhengque")
    40. score += 1
    41. scoreLabel.text = "总得分:\(score)"
    42. }else{
    43. print("huidacuowu")
    44. }
    45. }else{
    46. if queastions[questionIndex].answer {
    47. print("huidacuowu")
    48. }else{
    49. print("huidazhengque")
    50. score += 1
    51. scoreLabel.text = "总得分:\(score)" }
    52. } }
    53. override func didReceiveMemoryWarning() {
    54. super.didReceiveMemoryWarning()
    55. // Dispose of any resources that can be recreated.
    56. }
    57. }

     4.制作弹窗

    https://github.com/relatedcode/ProgressHUD

    gitHub上拉的swift文件拖到项目中去。

     在ViewController中调用这个swift中的方法。

    1. import UIKit
    2. class ViewController: UIViewController {
    3. var questionIndex = 0
    4. var score = 0
    5. @IBOutlet weak var questionLabel: UILabel!
    6. @IBOutlet weak var scoreLabel: UILabel!
    7. @IBOutlet weak var progressLable: UILabel!
    8. @IBOutlet weak var progressBarViewWidth: NSLayoutConstraint!
    9. override func viewDidLoad() {
    10. super.viewDidLoad()
    11. // Do any additional setup after loading the view, typically from a nib.
    12. questionLabel.text = queastions[0].text
    13. }
    14. @IBAction func answerPressed(_ sender: UIButton) {
    15. checkAnswer(sender.tag)
    16. questionIndex += 1
    17. nextQuestion()
    18. progressLable.text = "\(questionIndex + 1) / 13"
    19. progressBarViewWidth.constant = (view.frame.width / 13) * CGFloat(questionIndex)
    20. }
    21. func nextQuestion(){
    22. if questionIndex <= 12{
    23. questionLabel.text = queastions[questionIndex].text
    24. }else{
    25. questionIndex = 0
    26. let alert = UIAlertController(title: "漂亮!", message: "您已经完成了所有问题,要再来一遍吗?", preferredStyle: .alert)
    27. let action = UIAlertAction(title: "再来一遍", style: .default, handler: { _ in
    28. self.questionLabel.text = queastions[0].text
    29. self.scoreLabel.text = "总得分:0"
    30. })
    31. alert.addAction(action)
    32. //
    33. present(alert, animated: true)
    34. }
    35. }
    36. func checkAnswer(_ tag: Int){
    37. if tag == 1 {
    38. if queastions[questionIndex].answer {
    39. ProgressHUD.showSucceed("答对了")
    40. score += 1
    41. scoreLabel.text = "总得分:\(score)"
    42. }else{
    43. ProgressHUD.showError("答错了")
    44. }
    45. }else{
    46. if queastions[questionIndex].answer {
    47. ProgressHUD.showError("答错了")
    48. }else{
    49. ProgressHUD.showSucceed("答对了")
    50. score += 1
    51. scoreLabel.text = "总得分:\(score)" }
    52. } }
    53. override func didReceiveMemoryWarning() {
    54. super.didReceiveMemoryWarning()
    55. // Dispose of any resources that can be recreated.
    56. }
    57. }

     5.启动测试

  • 相关阅读:
    nodeJs+jwt实现小程序tonken鉴权
    Python语句和循环
    数据库生产架构( 二 ) 分库分表
    卷积神经网络(包含代码与相应图解)
    springcloud、springboot、springcloudalibaba版本依赖关系
    wireguard windows版本的配置
    作业调度FluentScheduler的使用
    Java深拷贝与浅拷贝技术解析及实例演示
    YOLOv5算法改进--通过yaml文件添加注意力机制【附代码】
    2022 极术通讯-Arm 虚拟硬件加速物联网软件开发
  • 原文地址:https://blog.csdn.net/LYly_B/article/details/132633944