- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
即使在按下主页按钮时我处于后台,我也想继续计时。我该怎么做?
这是我的工作代码,它是锻炼的计时器。计数完成后我正在使用闹钟:
import UIKit
import AVFoundation
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
var pickerInfo: [String] = []
var tempsCuisson:Int = 0
var timer:Timer = Timer()
var lecteur:AVAudioPlayer = AVAudioPlayer()
var estActif:Bool = false
var selection:Int?
//outlets
@IBOutlet weak var minuteurLabel: UILabel!
override func viewDidAppear(_ animated: Bool) {
minuteurLabel.text = minuteurString(temps: tempsCuisson)
}
@IBOutlet weak var activerMinuteurBtn: UIButton!
@IBOutlet weak var pickerView: UIPickerView!
@IBOutlet weak var navBar: UINavigationBar!
//actions
@IBAction func activerMinuteurAction(_ sender: UIButton) {
compteur()
}
@IBAction func resetMinuteurAction(_ sender: UIButton) {
resetCompteur()
}
override func viewDidLoad() {
super.viewDidLoad()
//datasource + delegate
pickerView.dataSource = self
pickerView.delegate = self
pickerInfo = ["00.15", "00.30", "00:45",
"01:00", "01:15", "01:30", "01:45",
"02:00", "02:15", "02:30", "02:45",
"03:00", "03:15", "03:30", "03:45",
"04:00", "04:15", "04:30", "04:45",
"05:00", "05:15", "05:30", "05:45",
"06:00", "06:15", "06:30", "06:45",
"07:00", "07:15", "07:30", "07:45",
"08:00", "08:15", "08:30", "08:45",
"09:00", "09:15", "09:30", "09:45", "10:00"
]
activerMinuteurBtn.setTitleColor(UIColor.white, for: UIControlState.normal)
activerMinuteurBtn.isEnabled = false
activerMinuteurBtn.alpha = 0.3
alarm()
}
func selectionCuisson(selection: Int) {
var titreVC:String?
switch selection {
case 0:
//code
tempsCuisson = 015
minuteurLabel.text = minuteurString(temps: tempsCuisson)
navBar.topItem?.title = titre(str: pickerInfo[selection])
break
case 1:
//code
tempsCuisson = 030
minuteurLabel.text = minuteurString(temps: tempsCuisson)
navBar.topItem?.title = titre(str: pickerInfo[selection])
break
case 2:
//code
tempsCuisson = 045
minuteurLabel.text = minuteurString(temps: tempsCuisson)
navBar.topItem?.title = titre(str: pickerInfo[selection])
break
case 3:
//code
tempsCuisson = 060
minuteurLabel.text = minuteurString(temps: tempsCuisson)
navBar.topItem?.title = titre(str: pickerInfo[selection])
break
all cases...
default:
//code
print("Aucune sélection")
break
}
//pour afficher option sélectionnée dans barre navigation
//self.title = titreVC
activerMinuteurBtn.isEnabled = true
activerMinuteurBtn.alpha = 1
minuteurLabel.textColor = UIColor.black
}
func minuteurString(temps: Int) -> String {
let minutes = Int(temps) / 60 % 60
let secondes = Int(temps) % 60
return String(format: "%02i:%02i", minutes, secondes)
}
func compteur() {
if (!estActif) {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.incrementer), userInfo: nil, repeats: true)
timer.fire()
activerMinuteurBtn.setTitle("STOP", for: UIControlState.normal)
activerMinuteurBtn.setTitleColor(UIColor.orange, for: UIControlState.normal)
estActif = true
} else {
timer.invalidate()
activerMinuteurBtn.setTitle("Démarrer", for: UIControlState.normal)
activerMinuteurBtn.setTitleColor(UIColor.blue, for: UIControlState.normal)
estActif = false
}
}
func incrementer() {
if (tempsCuisson == 0) {
timer.invalidate()
minuteurLabel.text = "00:00"
activerMinuteurBtn.setTitle("Démarrer", for: UIControlState.normal)
activerMinuteurBtn.setTitleColor(UIColor.blue, for: UIControlState.normal)
minuteurLabel.textColor = UIColor.green
activerMinuteurBtn.isEnabled = false
activerMinuteurBtn.alpha = 0.3
lecteur.play()
} else {
tempsCuisson -= 1
minuteurLabel.text = minuteurString(temps: tempsCuisson)
}
}
func resetCompteur() {
timer.invalidate()
tempsCuisson = 0
minuteurLabel.text = "00:00"
activerMinuteurBtn.setTitle("Démarrer", for: UIControlState.normal)
activerMinuteurBtn.setTitleColor(UIColor.white, for: UIControlState.normal)
estActif = false
activerMinuteurBtn.isEnabled = false
activerMinuteurBtn.alpha = 0.3
pickerView.selectRow(0, inComponent: 0, animated: true)
}
//AVAudioPlayer
func alarm() {
DispatchQueue.global(qos: .userInitiated).async {
let fichier = Bundle.main.path(forResource: "alarm", ofType: "mp3")
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, with:[.duckOthers])
try AVAudioSession.sharedInstance().setActive(true)
try self.lecteur = AVAudioPlayer(contentsOf: (URL(string: fichier!))!)
} catch {
print("erreur lecture ficher MP3")
}
}
}
//Retourner Titre
func titre(str:String) -> String {
return str
}
//MARK - PickerViewDataSource
// returns the number of 'columns' to display.
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
// returns the # of rows in each component..
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerInfo.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerInfo[row]
}
//changer couleur pickerView label
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: pickerView.frame.size.width, height: 44))
label.textColor = UIColor.white
label.font = UIFont(name: "HelveticaNeue-Bold", size: 22)
label.textAlignment = .center
label.text = String(format:" %@", pickerInfo[row])
if (row == selection) {
label.textColor = UIColor.yellow
}
return label
}
//MARK - PickerViewDelegate
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selectionCuisson(selection: row)
selection = row
}
}
最佳答案
好吧,无需深入研究您的代码,您似乎正在使用 Timer.scheduledTimer
每秒手动递减用户设置的时间。
正如您所发现的那样,这不是一项好技术 - 只有当您知道您可以绝对控制应用程序的时间时,它才有效。
相反,您应该做的是存储用户启动警报的时间、预计的结束时间,并运行计时器以定期更新 UI。
(我的代码在这里并不完美,但它应该为您指明正确的方向,以解决让计时器在后台运行的问题。)
例如
class ViewController: UIViewController {
// this is pseudo-code as I don't have my compiler open :(
let start: Date!
let end: Date!
func selectionCuisson(selection: Int) {
...
start = Date()
end = Date(timeInterval: tempsCuisson, since: start)
}
}
然后您创建一个计时器,它只会更新 UI。
// You can set this to be faster than the increment, for a smoother UI experience
// put in compteur()? I think
timer = Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(ViewController.incrementer), userInfo: nil, repeats: true)
timer.fire()
...
func incrementer() {
let tempsCuisson = end - start
if tempsCuisson < 0 {
// End your Timing Function here
timer.invalidate()
...
lecteur.play()
} else {
minuteurLabel.text = minuteurString(temps: tempsCuisson)
}
}
您还可以使用 end
日期将本地通知设置为在应用程序进入后台时关闭
// when the app becomes inactive
let notification = UILocalNotification()
...
notification.fireDate = end
UIApplication.shared.scheduleLocalNotification(notification)
关于ios - Swift 3 - 如何让我的计时器在后台计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43330907/
我需要在 文档就绪 触发后 5 秒调用我的函数 abc()。 这在 jQuery 中可能吗? $(document).ready(function () { //Wait 5 seconds then
我发现 System.Diagnostics.Stopwatch 类即使在很短的时间段(即 20 秒)内似乎也存在可测量的不准确性。对于编码为运行 20 秒的进程,我的进程显示耗时为 20.3+ 秒:
我正在使用 Ionic Framework 使用 Angular 构建一个 Android/iPhone 食谱应用程序。 功能之一应该是每个成分/步骤的警报/计时器。 我猜 Ionic 是基于 Apa
我有一个 JS 计时器脚本,从 20 秒开始倒计时 var count = 0; var speed = 1000; countdown = setInterval( function
我的 JavaScript 计时器有一个大问题。一切正常,除了 1 个按钮:停止!我不知道如何停止计数器上的所有内容(例如重置每个输入和计数器)。有什么想法可以解决这个问题吗?
所以我有一个 TimerTask 任务 在其 run() 中调用函数 onTimerComplete() onTimerComplete() 看起来像这样: private void onTimerC
我在 Eclipse 中的 windowbuilder 创建的 run() 方法中有计时器。 我不知道如何通过另一种方法停止该计时器。例如通过添加 if(MenuWindow.gameStarted
我对java很陌生,我试图在按下按钮时启动计时器,但无法启动它。我尝试了几行代码,但似乎没有任何效果。 我已经声明了我的标签、文本字段和计时器: private JTextField JTFHours
import java.util.Scanner; import java.util.Timer; import java.util.TimerTask; public class Boggle {
我正在尝试在 JavaScript 中获取计时器的值,因此当计时器为零时它会显示不同的内容; var local_t = new Date(); local_t.setSeconds(local_t.
我需要使用 jquery 为网站创建自定义 slider 。到目前为止,如果我单击按钮,我已经设法让它按照我想要的方式运行,但我想实现一个自动计时器,以便幻灯片在 5 秒后切换。 这是我的 JS 代码
我正在制作一个计时器,记录点击“通过”按钮上的“确定”时的最长时间(个人最好成绩)。我似乎无法弄清楚如何让计数器回到 0,我确信有一种方法可以只用 2 个按钮(开始 + 通过)来完成。我希望计时器在我
我有一个 java.util.Timer 用于限制电子邮件发送(如果最近发送了状态电子邮件,那么现在不要发送,而是创建一个计时器任务以稍后发送任何新状态)。 我想要的是确保最终发送所有排队的电子邮件,
我已经建立了一个在线考试门户,允许学生使用网络浏览器在线参加考试。 现在我还开发了计时器功能,用户必须在规定的时间范围内完成考试。我遇到的麻烦是,当我刷新页面时,计时器再次从新开始,例如 40 分钟。
我想知道在一定时间内禁用按钮的最佳方法是什么。当用户使用其他按钮转到其他页面时,定时器不会被重置,按钮仍将被禁用。 我读过很多关于异步任务、线程的不同方法。但是我非常不确定哪种方法最好。 我希望计时器
我正在制作一个测验应用程序,我想在其中显示用户在玩游戏时所用的时间。它应该采用 HH:MM:SS 格式,从 00:00:00 开始,直到他选择答案。当用户每秒播放时,计时器应该每秒更新一次。另外,我想
您好,计划为该 Activity 开发 android 倒数计时器应用程序,当用户单击开始按钮时显示计时器倒计时并显示计时器倒计时,用户将转到剩余 Activity ,即使计时器在后台运行,如果用户单
我想知道是否有一种简单的方法可以使用 PHP 在数据库中创建计时器,我想在数据库中创建该行 30 分钟后将其删除。如果这不够具体,我很抱歉,但我不知道如何在其中提供更多细节,请随意发表评论,以便我可以
我试图制作一种与我的计时器一起使用的对象。问题是,当我只有裸函数(不在对象中)时,它就可以工作。但是当我把它放在对象内部时它不起作用。 使用此代码我只能看到 00:01 当我只使用函数本身时,它工作正
这个问题已经有答案了: How to format numbers as currency strings (67 个回答) 已关闭 9 年前。 我想显示从 1 到 2500 的美元计时器。当计时器增
我是一名优秀的程序员,十分优秀!