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

news/2024/7/20 23:10:48 标签: ios, swift, 开发语言

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.启动测试


http://www.niftyadmin.cn/n/4995680.html

相关文章

LeetCode:1143. 最长公共子序列 - Python

1143. 最长公共子序列 问题描述&#xff1a; 给定两个字符串 text1 和 text2&#xff0c;返回这两个字符串的最长 公共子序列 的长度。如果不存在公共子序列 &#xff0c;返回 0 。 一个字符串的 子序列 是指这样一个新的字符串&#xff1a;它是由原字符串在不改变字符的相对…

FLASH读写数据

目录 嵌入式 Flash大概了解 数据手册2.3.2章节 结构图f407 等待周期 Flash 控制寄存器解锁 编程/擦除并行位数 擦除 编程&#xff08;写入&#xff09; 工程程序 嵌入式 Flash大概了解 可以从flash区域启动程序&#xff1b;大概是程序区可以在flash&#xff0c;所以是可以…

springboot之一:配置文件(内外部配置优先顺序+properties、xml、yaml基础语法+profile动态切换配置、激活方式)

配置的概念&#xff1a; Spring Boot是基于约定的&#xff0c;所以很多配置都有默认值&#xff0c;但如果想使用自己的配置替换默认配置的话&#xff0c;就可以使用application.properties或者application.yml(application.yaml)进行配置。 注意配置文件的命名必须是applicat…

DevEco Studio 配置

首先,打开deveco studio 进入首页 …我知道你们想说什么,我也想说 汉化配置 没办法,老样子,先汉化吧,毕竟母语看起来舒服 首先,点击软件左下角的configure,在配置菜单里选择plugins 进入到插件页面, 输入chinese,找到汉化插件,(有一说一写到这我心里真是很不舒服) 然后点击o…

C++之map,set,multimap,multiset的使用

map&#xff0c;set&#xff0c;multimap&#xff0c;multiset的使用 关联式容器键值对树形结构的关联式容器setset介绍set的使用set定义方式set各种操作函数 multiset mapmap的介绍map的使用insert函数find函数erase函数[ ]运算符重载map的迭代器遍历 multimap 关联式容器 在…

WordPress(4)关于网站的背景图片更换

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言一、更改的位置1. 红色区域是要更换的随机的图片二、替换图片位置三.开启随机数量四.结束前言 提示:这里可以添加本文要记录的大概内容: 例如:随着人工智能的不断发展,机器学习这门技术也…

前端面试准备学习记录 — CSS篇

2.1、CSS基础 选择器&#xff1a;id > 类 属性 伪类 > 标签 优先级&#xff1a;内联样式 > id > 类、伪类、属性 > 标签 !important优先级最高 block&#xff1a;独占一行&#xff1b;inline&#xff1a;width、height属性无效&#xff0c;水平方向的margin和…

简易版剪辑视频程序(python-VideoFileClip)

很多剪辑软件要收费&#xff0c;自己写了一个。 from moviepy.video.io.VideoFileClip import VideoFileClipdef trim_video(input_path, output_path, trim_duration):video_clip VideoFileClip(input_path)trimmed_clip video_clip.subclip(0, video_clip.duration - trim…