tengo un proyecto entre manos en el que tengo que manipular un elemento del storyboard llamado progressView, el cual lo uso para medir el tiempo que se tarda en pasar de una pantalla en mi app, primero inicializo un Timer para luego usarlo con el progressView.progress
y me gustaría saber cual sería la mejor manera de coger el valor de ese Timer y mantenerlo en el progressView cuando se produzca una acción del usuario.
`import UIKit
class PreguntaViewController: UIViewController {
@IBOutlet var quest: UILabel!
@IBOutlet var value: UILabel!
@IBOutlet var request: UITextField!
@IBOutlet var viewRequestGeneral: UIView!
@IBOutlet var viewRequestCorrect: UIView!
@IBOutlet var viewRequestError: UIView!
@IBOutlet var viewRequestIcon1: UIView!
@IBOutlet var viewRequestIcon2: UIView!
@IBOutlet var buttonRequestIcon1: UIButton!
@IBOutlet var buttonRequestIcon2: UIButton!
@IBOutlet weak var progressView: UIProgressView!
var timer = Timer()
let button = UIBarButtonItem.init(title: "next", style: .done, target: self, action: #selector(buttonAction))
var colors: [String:UIColor] = ["Negro":UIColor.black, "Rojo":UIColor.red, "Azul":UIColor.blue, "Verde":UIColor.green, "Naranja":UIColor.orange, "Amarillo":UIColor.yellow, "Marron":UIColor.brown, "Gris":UIColor.gray]
override func viewDidLoad() {
super.viewDidLoad()
timer = Timer.scheduledTimer(timeInterval: 0.0015, target: self, selector: #selector(timeOut), userInfo: nil, repeats: true)
let button = UIBarButtonItem.init(title: "Submit", style: .done, target: self, action: #selector(buttonAction))
self.navigationItem.rightBarButtonItem = button
value.textColor = colors.values.shuffled().first
value.text = colors.keys.shuffled().first
viewRequestError.layer.cornerRadius = 10.0
viewRequestCorrect.layer.cornerRadius = 10.0
viewRequestIcon1.layer.cornerRadius = 10.0
viewRequestIcon2.layer.cornerRadius = 10.0
buttonRequestIcon1.layer.cornerRadius = 5.0
buttonRequestIcon2.layer.cornerRadius = 5.0
request.layer.cornerRadius = 5.0
request.adjustsFontSizeToFitWidth = true
request.layer.borderWidth = 1
}
@objc func timeOut() {
progressView.progress += 0.0001
if viewRequestCorrect.alpha == 1 {
if progressView.progress == 1 {
actionViewRequestError()
}
}
}
func actionViewRequestCorrect() {
self.viewRequestError.alpha = 0
self.viewRequestCorrect.alpha = 0.75
self.viewRequestGeneral.alpha = 0.75
self.viewRequestGeneral.inputView
// aqui se llama a la func que le suma un vlor a la pantalla de resultado. hay que crear un view controller para dicha pantalla. }
func actionViewRequestError() {
self.viewRequestCorrect.alpha = 0
self.viewRequestError.alpha = 0.75
self.viewRequestGeneral.alpha = 0.75
self.viewRequestGeneral.inputView
}
@IBAction func buttonClear() {
let storyboard = UIStoryboard.init(name: "Main", bundle: Bundle.main)
let preguntaViewController = storyboard.instantiateViewController(withIdentifier: "Pregunta")
self.navigationController?.pushViewController(preguntaViewController, animated: true)
}
@objc func buttonAction() {
let valueColor = colors[request.text!.capitalized]
if valueColor == value.textColor {
actionViewRequestCorrect()
} else {
actionViewRequestError()
}
}
// aqui una funcion que le de un valor a el modelo de la pantalla de resultado.
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
} `
var timer = Timer() timer = Timer.scheduledTimer(timeInterval: 0.0015, target: self, selector: #selector(timeOut), userInfo: nil, repeats: true) let button = UIBarButtonItem.init(title: "Submit", style: .done, target: self, action: #selector(buttonAction)) self.navigationItem.rightBarButtonItem = button unc timeOut() { progressView.progress += 0.0001 if viewRequestCorrect.alpha == 1 { if progressView.progress == 1 { actionViewRequestError()}}}