我正在努力完成这项工作。我想要一个按钮,一旦按下它一段时间就会被禁用。一旦该时间结束,该按钮将再次启用,直到再次按下它,然后在设定的时间内禁用它。我不希望通过重新启动应用程序轻松再次启用该按钮。它必须保持禁用状态,直到时间结束,直到下一次启动应用程序.. 到目前为止,我的代码只能禁用一次按钮。它不起作用,在时间过去后再次启用它。在我重新启动应用程序后,该按钮也会再次启用。这不应该发生。
有人有什么想法,我怎样才能让它按照我想要的方式工作?
这是我的代码:
button.isUserInteractionEnabled = false
Timer.scheduledTimer(withTimeInterval: 5, repeats: true, block: { _ in
button.isUserInteractionEnabled = false
})
我也这样试过:
_ = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(fireTimer), userInfo: nil, repeats: true)
_ = Timer.scheduledTimer(timeInterval: 0.0, target: self, selector: #selector(fireTimer2), userInfo: nil, repeats: false)
@objc 是:
@objc func fireTimer() {
print("Timer fired!")
}
@objc func fireTimer2() {
print("Start timer.")
}
有了这个,我想尝试一下,如果我可以设置开始时间和重复时间,希望通过按钮使事情顺利进行。我没有。 有人可以帮我吗?
非常感谢!一切顺利
更新,6 月 29 日:
这是代码,我用于我的按钮并使我想要的禁用/启用功能起作用。
@objc private let actionButton: UIButton = {
let button = UIButton()
button.setTitle("Button Title", for: .normal)
button.titleLabel?.font = UIFont(name: "Times New Roman", size: 16)
button.setTitleColor(.secondaryLabel, for: .normal)
button.addTarget(self, action: #selector(didTapButton), for: .touchDown)
return button
}()
@objc func didTapButton() {
self.actionButton.isUserInteractionEnabled = false
Timer.scheduledTimer(withTimeInterval: 20, repeats: true) { _ in
self.actionButton.isUserInteractionEnabled = true
}}
那么如何将计时器中的数据存储在 core data
(???) 或 UserDefaults
(???) 或任何地方,以便在重新启动应用程序时计时器不会重置?
答案 0 :(得分:1)
Timer
类有一个初始值设定项 init(fire:interval:repeats:block)
,允许您设置 Date
何时触发。这对您的问题很有用。你必须小心一点,确保你不要忘记安排计时器,否则它永远不会触发。
你可以用它来写一个这样的方法:
func disable(button buttonToDisable: UIButton, until date: Date) {
buttonToDisable.isEnabled = false
let timer = Timer.init(fire: date, interval: 0, repeats: false) { _ in
buttonToDisable.isEnabled = true
}
RunLoop.current.add(timer, forMode: .common)
}
此方法在给定日期之前禁用指定按钮。请注意,为 false
参数传递了 repeats
。我们只希望它运行一次。
有了这个方法,您现在可以在按下按钮时执行以下操作:
let disabledTimeInterval = TimeInterval(30.0)
@IBAction func buttonPressed(_ sender: Any) {
let disabledUntil = Date().advanced(by: disabledTimeInterval)
disable(button: self.button, until: disabledUntil)
}
这负责在应用运行时启用和禁用按钮。但是您也提到您不希望用户能够停止和启动您的应用程序来绕过超时。您可以通过保存按钮应该被禁用的日期直到 UserDefaults
来处理这个问题。以下代码显示了如何将 Date
保存和加载到 UserDefaults
。
let buttonDisabledUntilDateKey = "buttonDisabledUntil"
func save(disabledUntilDate date: Date) {
UserDefaults.standard.set(date, forKey: buttonDisabledUntilDateKey)
}
func loadDisabledUntilDate() -> Date? {
return UserDefaults.standard.object(forKey: buttonDisabledUntilDateKey) as? Date
}
您现在可以修改 buttonPressed
方法来保存此数据:
@IBAction func buttonPressed(_ sender: Any) {
let disabledUntil = Date().advanced(by: disabledTimeInterval)
save(disabledUntilDate: disabledUntil)
disable(button: self.button, until: disabledUntil)
}
最后,当您的视图即将出现时,您应该检查当前时间是否早于应该重新启用按钮的时间,并在必要时禁用它。
override func viewWillAppear(_ animated: Bool) {
let now = Date()
if let buttonDisabledUntilDate = loadDisabledUntilDate(),
now < buttonDisabledUntilDate {
disable(button: self.button, until: buttonDisabledUntilDate)
}
}