计时器不会使swift 4失效

计时器不会使swift 4失效,swift,timer,Swift,Timer,我看到一些帖子提到这个问题,其中启动多个计时器可能是个问题。然而,我不认为我自己开始不止一个,也许我只是错过了什么?我对这个很陌生。现在,如果用户的速度超过8 mps,我会尝试启动计时器,并保持它运行,直到他停止足够长的时间,让计时器耗尽。当前,即使满足使计时器无效的条件,计时器仍会继续倒计时 非常感谢您的关注,这里的帮助总是非常感谢 import UIKit class ViewController: UIViewController, { //Setup timer va

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

非常感谢您的关注,这里的帮助总是非常感谢

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
                }
            })
        }

    }

}

您的问题在于代码的这一部分

Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(timeoutPeriod)), userInfo: nil, repeats: true)
您正在创建计时器,但没有将其设置到您创建的
var Timer=Timer()

要解决此问题,您只需正确设置
计时器
变量

self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(timeoutPeriod)), userInfo: nil, repeats: true)
为什么对我来说: 首先在
DidLoad()
运行缓慢,在
increasInt=9
停止。 第二次由代码触发以使其自动执行:
reset increasInt=-1-
它运行得更快 并在
increasInt>11之后继续

func aniDrag(ani: Double){
        dragTimer = Timer.scheduledTimer(timeInterval: ani, target: self, selector: #selector(updatedrag), userInfo: nil, repeats: true)
           }
       @objc  func updatedrag() { // dauIndex
           increasInt = increasInt + 1
        if (increasInt > -1 ) && ( increasInt < 10 ) {
           aniImage.image = UIImage(named: "ani" +  allLessonArray[realnIndex] + String(increasInt))!
            print(allLessonArray[realnIndex] + String(increasInt) )
        }
        if increasInt == 10 {
            if  dragTimer != nil {
                print("Timer Stop !!!!!")
                dragTimer.invalidate()}
                   }

        if increasInt > 11 {  print(increasInt)}
        }
   //test aniDrag(ani: 0.5)
func aniDrag(ani:Double){
dragTimer=Timer.scheduledTimer(时间间隔:ani,目标:self,选择器:#选择器(updatedrag),userInfo:nil,repeats:true)
}
@objc func updatedrag(){//索引
递增=递增+1
如果(递增>-1)和(递增<10){
aniImage.image=UIImage(名为:“ani”+allLessonArray[realnidex]+String(increasInt))!
打印(AllleSonaray[realnIndex]+字符串(递增))
}
如果递增==10{
如果dragTimer!=零{
打印(“计时器停止!!!!!!!”)
dragTimer.invalidate()}
}
如果递增>11{打印(递增)}
}
//测试ani阻力(ani:0.5)

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

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

非常感谢,这就是问题所在:D