gpt4 book ai didi

ios - 按下主页按钮时如何使ios应用程序停止

转载 作者:行者123 更新时间:2023-11-28 15:09:10 24 4
gpt4 key购买 nike

<分区>

我的应用程序上有一个页面,当按下按钮时它会运行一个计数器。当我转到另一个页面时,或者当应用程序通过双击主屏幕并将其滑开而完全终止时,它会停止运行。这对我来说很好,正是我想要它做的。即使应用程序仍在运行,我也希望当我按下主页按钮时计数器停止,我希望计数器停止。我会在下面发布我的计数器。

class Miner: UIViewController {

//Start
@IBOutlet weak var startMining: UIButton!

//Coin Label
@IBOutlet weak var coinLabel: UILabel!

//Counter
var count:Int {
get {
return UserDefaults.standard.integer(forKey: "count")
}
set {
UserDefaults.standard.set(newValue, forKey: "count")
coinLabel.text = "Coins: 0. \(newValue)"

}
}
var counting:Bool = false
var timer:Timer = Timer()




override func viewDidLoad() {


super.viewDidLoad()


}

@objc func counter() -> Void {

count += 1
coinLabel.text = "Coins: 0." + String(count)
walletLabel.text = "Wallet: 0." + String(count)

}

@IBAction func startMining(_ sender: Any) {
if counting {
// Stop Counting
startMining.setTitle("Start", for: .normal)

timer.invalidate()

counting = false


} else if !counting {
// Start Counting
startMining.setTitle("Stop", for: .normal)
// Start timer
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector (counter), userInfo: nil, repeats: true)
counting = true
}



}

}

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