ViewController.swift 1.69 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
//
//  ViewController.swift
//  NuevoProyecto
//
//  Created by Pablo Perez on 2021-08-02.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var boton: UIButton!
    @IBOutlet weak var tiempo: UILabel!
    @IBOutlet weak var veces: UILabel!
    var cont = 0
    
    @IBAction func botonAction(_ sender: Any) {
        cont = cont + 1
        veces.text = String (cont)
    }
    
    var timer = Timer()
    var requestTimer = 10

    @objc func timeMethod(){
        requestTimer -= 1
        tiempo.text = String (requestTimer)
        if requestTimer == 5 {
            timer.invalidate()
            let alert = UIAlertController(title: "Juego Terminado", message: "Total de puntos: \(String(describing: veces.text!))", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "Aceptar", style: .default, handler: {
                (alert:UIAlertAction!) in self.startGame()
            }))
            self.present(alert, animated: true)
            cont = 0
            veces.text = String (cont)
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        startGame()
        
//        boton.center.x = view.frame.width / 2
//        boton.center.y = view.frame.height / 4
//        boton.addTarget(self, action: #selector(aleatorio), for: .touchUpInside)
    }
    
    func startGame(){
        requestTimer = 10
        tiempo.text = String (requestTimer)
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timeMethod), userInfo :nil, repeats: true)
    }
    
//    @objc func aleatorio(){
//        boton.center.x = boton.frame.width / 2
//        boton.center.y = boton.frame.height / 2
//    }

}