我正在尝试在Mapbox的标签上显示实时倒计时数据。当前,用户需要通过以下方式“刷新”地图:在标签外单击,然后再次单击标签或终止应用程序以显示更新的倒计时信息。标签升起时有没有办法显示实时倒计时?
import Mapbox
class ViewController: UIViewController, MGLMapViewDelegate {
var countdownTimer: Timer!
var totalTime = 5000
let test = MGLPointAnnotation()
override func viewDidLoad() {
super.viewDidLoad()
startTimer()
check()
}
func startTimer() {
countdownTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
}
@objc func updateTime() {
let(h,m,s) = self.secondsToHoursMinutesSeconds(seconds: self.totalTime)
var hours = h
var min = m
var seconds = s
test.subtitle = "time Remaining: \(hours) hours \(min)minutes \(seconds)seconds "
if totalTime != 0 {
totalTime -= 1
} else {
endTimer()
}
}
func endTimer() {
countdownTimer.invalidate()
}
func check(){
let mapView = MGLMapView(frame: view.bounds)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(mapView)
mapView.delegate = self
mapView.showsUserLocation = true
mapView.setUserTrackingMode(.follow, animated: true)
self.test.coordinate = CLLocationCoordinate2D(latitude: -157.222, longitude: 24.444)
self.test.title = "test"
mapView.addAnnotation(self.test)
}
func secondsToHoursMinutesSeconds (seconds : Int) -> (Int, Int, Int) {
return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)
}
func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
return nil
}
func mapView(_ mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
return true
}
}