如何设置计时器以启用/禁用按钮 [英] How to set a timer to enable/disable buttons

查看:173
本文介绍了如何设置计时器以启用/禁用按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题. 我想设置一个计时器来禁用 ViewController2 的6个大按钮.

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

例如:在计时器达到100之前,无法单击按钮Clue1Button,Clue2Button,Clue3Button,Clue4Button,Clue5Button,Clue6Button 在计时器达到200之前,无法单击按钮2、3、4、5、6 ...

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

我的 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

推荐答案

您可能想做的是:

//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
}

这篇关于如何设置计时器以启用/禁用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆