How to set a timer to enable/disable buttons

对着背影说爱祢 提交于 2020-01-22 03:39:08

问题


I got an issue. I want to set a timer that could disable the 6 large buttons of my ViewController2.

For example : Until the timer reach 100, it's not possible to click on the buttons Clue1Button,Clue2Button,Clue3Button,Clue4Button,Clue5Button,Clue6Button Until the timer reach 200, it's not possible to click on the buttons 2,3,4,5,6 ...

How should I do it ? I tried several times, but I failed each time. Thanks for your help

Code of my ViewController2 :

//  ViewController2.swift
//  PROJET X
//
//  Created by Alexis Decloedt on 22/12/2019.
//  Copyright © 2019 Alexis Decloedt. All rights reserved.
//

import UIKit




class ViewController2: UIViewController {
    @IBOutlet weak var Clue1Button: UIButton!
    @IBOutlet weak var Clue2Button: UIButton!
    @IBOutlet weak var Clue3Button: UIButton!
    @IBOutlet weak var Clue4Button: UIButton!
    @IBOutlet weak var Clue5Button: UIButton!
    @IBOutlet weak var Clue6Button: UIButton!


    override func viewDidLoad() {
        super.viewDidLoad()


        // Do any additional setup after loading the view.
    }

  @IBAction func ChestButton(_ sender: Any) {
        dismiss(animated: false, completion: nil)
    }

    }
//
//  ViewController2.swift
//  PROJET X
//
//  Created by Alexis Decloedt on 22/12/2019.
//  Copyright © 2019 Alexis Decloedt. All rights reserved.
//

import UIKit

var currentCount : Int? 
var maxCount : Int?
var mytimer : Timer?
let ValeurStock = "ValeurStock"


class ViewController2: UIViewController {
    @IBOutlet weak var Clue1Button: UIButton!
    @IBOutlet weak var Clue2Button: UIButton!
    @IBOutlet weak var Clue3Button: UIButton!
    @IBOutlet weak var Clue4Button: UIButton!
    @IBOutlet weak var Clue5Button: UIButton!
    @IBOutlet weak var Clue6Button: UIButton!


    override func viewDidLoad() {
        super.viewDidLoad()

    currentCount = 0

        self.mytimer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(increase), userInfo: nil, repeats: true)


    }

  @IBAction func ChestButton(_ sender: Any) {
        dismiss(animated: false, completion: nil)

    }

    func increase() {
        currentCount! += 1

    }

        func buttonStatus() {
            if currentCount ?? <#default value#> >= 100 {
            //I'm stuck ?? How to continue ?
            }
        }


    }


Try to make my button disable/enable

回答1:


What you may want to do is:

//Add variable that will count how many times Timer was repeated.
var timerCount = 0

//Create timer
Timer.scheduledTimer(withTimeInterval: 100, repeats: true) { t in

    //After first 100 sec passed, timer will get triggered and enable the buttons you want.
    button1.isEnabled = true
    button2.isEnabled = true

    //After another 100 passed, timer will enable other buttons
    if timerCount == 1 {
        button3.isEnabled = true
        button4.isEnabled = true
        t.invalidate()
    }

    //Adds 1 to timer count
    timerCount += 1
}


来源:https://stackoverflow.com/questions/59461583/how-to-set-a-timer-to-enable-disable-buttons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!