簡體   English   中英

計時器不會使Swift 4失效

[英]Timer will not Invalidate swift 4

我看到幾個帖子提到了這個問題,其中可能啟動多個計時器。 但是,我看不到自己的起點超過一個,也許我只是想念一些東西? 我對此很陌生。 現在,如果用戶的速度超過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
                }
            })
        }

    }

}

即使將其設置為n,正常的計時器功能也不會失效,但是以下功能幫助我解決了這個問題。 希望這可以幫助

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

您的問題出在代碼的這一部分。

Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(timeoutPeriod)), userInfo: nil, repeats: true)

您正在創建一個Timer,但沒有將其設置為您創建的Timer變量var timer = 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM