Git Product home page Git Product logo

Comments (6)

resand avatar resand commented on July 26, 2024 1

@ajeetpratap Use an observer when sending the app to the background and disable the timer, returning to the foreground subtractthe time difference and initialize the timer again.

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(willResignActive), name: .UIApplicationWillResignActive, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(didBecomeActive), name: .UIApplicationDidBecomeActive, object: nil)
}

@objc func willResignActive(_ notification: Notification) {
     currentBackgroundDate = Date()
     timer.invalidate()
}
    
@objc func didBecomeActive(_ notification: Notification) {
     let calender: Calendar = Calendar.current
     let components: DateComponents = calender.dateComponents([.year, .month, .day, .hour, .minute, .second], from: currentBackgroundDate!, to: Date())
     let seconds = components.second
     let secondsUpdate = countDownView.currentSecond - seconds!
        
     countDownView.start(beginingValue: secondsUpdate, interval: 1)
}

from srcountdowntimer.

app-web-dev avatar app-web-dev commented on July 26, 2024

Having same issue!

from srcountdowntimer.

resand avatar resand commented on July 26, 2024

@app-web-dev I solved it with observers!

from srcountdowntimer.

ajeetpratap avatar ajeetpratap commented on July 26, 2024

I am stuck with this same issue.. I want the timer to run even the app goes in background. @resand How you solved it with observer?

from srcountdowntimer.

ajeetpratap avatar ajeetpratap commented on July 26, 2024

@resand Thanks for the quick reply:
countDownView.start(beginingValue: secondsUpdate, interval: 1) will start the timer again and it will also update the UI also.I want to persist the UI and resume it again when I come to foreground.
Any thoughts on that?

from srcountdowntimer.

resand avatar resand commented on July 26, 2024

@ajeetpratap You could check if the library has a function to get the time difference and keep the UI.
He served me as an example.

from srcountdowntimer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.