Desarrollo de iOS Swift-7-Score, número de pregunta, objeto de restricción, cuadro de mensaje, método de clase y aplicación de prueba divertida de método estático

1. Calcule la puntuación según la respuesta del usuario.

 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. Muestra el número de serie del tema.

 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. Cambie las restricciones de la barra de progreso de la pantalla.

Arrastre una restricción de ancho de 1:13 al ViewController.

 Debido a que ProgressBarView es de solo lectura, es necesario calcular el ancho de 1/13 de acuerdo con el ancho de la pantalla y luego agregarlo a 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. Crea una ventana emergente

https://github.com/ relatedcode/ProgressHUD

Arrastre el archivo Swift extraído de gitHub al proyecto.

 Llame a este método rápido en ViewController.

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. Inicie la prueba

Supongo que te gusta

Origin blog.csdn.net/LYly_B/article/details/132633944
Recomendado
Clasificación