gpt4 book ai didi

swift - 定时器不会使swift 4无效

转载 作者:可可西里 更新时间:2023-11-01 00:34:19 25 4
gpt4 key购买 nike

我看到一些帖子提到这个问题,其中启动多个计时器可能是问题所在。但是,我不认为自己开始不止一个,也许我只是错过了什么?我对此很陌生。现在,如果用户的速度超过 8 英里/秒,我将尝试启动计时器,并保持它运行,直到他停止足够长的时间以使计时器用完。目前,即使满足使其失效的条件,计时器仍会继续倒计时。

非常感谢您的浏览,非常感谢这里的帮助

import UIKit

class ViewController: UIViewController, {

//Setup timer
var timer = Timer()
var seconds = 5
var timerRunning = false

//Timer countdown
@objc func timeoutPeriod() {
seconds -= 1
}


func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

if userLocation.speed > 8 && timerRunning == false {
//flip running to True and start timer
timerRunning = true
seconds = 10
Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(timeoutPeriod)), userInfo: nil, repeats: true)
geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
let placemark: CLPlacemark = placemarks![0]
if let city = placemark.locality {
self.startingLocation = city
print("Starting Location: " + city)
}
})
} else if userLocation.speed > 8 && timerRunning == true {
seconds = 10
} else if seconds == 0 {
//end timer (hopefully)
self.timer.invalidate()
geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
let placemark: CLPlacemark = placemarks![0]
if let city = placemark.locality {
self.currentDateString = self.dateFormatter.string(from: self.date)
self.endingLocation = city
print("Ending Location: " + city)
self.timerRunning = false
self.seconds = 10
}
})
}

}

}

最佳答案

正常的计时器功能即使设为 nil 也不会失效,但下面的功能帮助我解决了这个问题。希望这有帮助

 self.myTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { (timerValue) in
if self.myTimer != nil {
self.updateTimerLabel() // call the selector function here
}
})

关于swift - 定时器不会使swift 4无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46761922/

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