1. Crea un proyecto de aplicación de prueba
2. Cree un texto de pregunta, centrado horizontalmente.
Cree dos botones, azul y rojo, y colóquelos en Stack View. Asigne a StackView una restricción central horizontal y una restricción inferior, y establezca la distancia entre los dos botones en 20.
Establezca la relación entre la vista de la barra de progreso y la Vista segura para que tenga el mismo ancho. Establezca su proporción en 1:13.
3. Agregue la adaptación del modo oscuro al sistema (solo para iOS13 y superior)
Se agregó un nuevo perfil de color a Activos.
Establece los colores por separado para claros y oscuros.
Seleccione todo el texto y configúrelo en un color llamado Color.
Efecto:
4. Utilice la idea MVC para codificar.
Cree un nuevo archivo .swift, Question.swift:
import Foundation
class Question {
var text: String
var answer: Bool
init(text: String, answer: Bool){
self.text = text
self.answer = answer
}
}
let queastions = [
Question(text: "马云是世界首富", answer: false),
Question(text: "刘强东最早是在中关村卖光盘的", answer: true),
Question(text: "苹果可以吃", answer: true),
Question(text: "只要坚持下去就能学好代码", answer: true),
Question(text: "王思聪是80后", answer: true),
Question(text: "在国内可以正常访问google", answer: false),
Question(text: "敲完一万行代码可以变身编程高手", answer: true),
Question(text: "撒贝宁说清华还行", answer: false),
Question(text: "一直学习变大牛", answer: true),
Question(text: "安卓也很好使", answer: true),
Question(text: "优酷比b站牛", answer: false),
Question(text: "上班可以摸鱼吗", answer: false),
Question(text: "这狗iOS系统真的没windows好用啊", answer: true)
]
ViewController.swift:
import UIKit
class ViewController: UIViewController {
var questionIndex = 0
@IBOutlet weak var questionLabel: 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) {
if sender.tag == 1 {
if queastions[questionIndex].answer == true{
print("huidazhengque")
}else{
print("huidacuowu")
}
}else{
if queastions[questionIndex].answer == true{
print("huidacuowu")
}else{
print("huidazhengque")
}
}
questionIndex += 1
if questionIndex <= 12{
questionLabel.text = queastions[questionIndex].text
}else{
questionIndex = 0
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
5. Crea una ventana emergente
ViewController.swift:
import UIKit
class ViewController: UIViewController {
var questionIndex = 0
@IBOutlet weak var questionLabel: 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) {
if sender.tag == 1 {
if queastions[questionIndex].answer == true{
print("huidazhengque")
}else{
print("huidacuowu")
}
}else{
if queastions[questionIndex].answer == true{
print("huidacuowu")
}else{
print("huidazhengque")
}
}
questionIndex += 1
if questionIndex <= 12{
questionLabel.text = queastions[questionIndex].text
}else{
let alert = UIAlertController(title: "漂亮!", message: "您已经完成了所有问题,要再来一遍吗?", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "再来一遍", style: .default, handler: { _ in
}))
self.present(alert, animated: true)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
6. Inicie la prueba
7. Perfecto de nuevo, organiza el código.
ViewController.swift:
import UIKit
class ViewController: UIViewController {
var questionIndex = 0
@IBOutlet weak var questionLabel: 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) {
if sender.tag == 1 {
if queastions[questionIndex].answer == true{
print("huidazhengque")
}else{
print("huidacuowu")
}
}else{
if queastions[questionIndex].answer == true{
print("huidacuowu")
}else{
print("huidazhengque")
}
}
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)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}