gpt4 book ai didi

ios - 如何设置计时器来启用/禁用按钮

转载 作者:行者123 更新时间:2023-11-29 05:13:15 24 4
gpt4 key购买 nike

我遇到一个问题。我想设置一个计时器,可以禁用我的 ViewController2 的 6 个大按钮。

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

我该怎么做?我尝试了几次,但每次都失败了。感谢您的帮助

我的代码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
}

关于ios - 如何设置计时器来启用/禁用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59474498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com