Adding Timers to Update UIView Background Color

804 Views Asked by At

Hi my fellow programmers, I'm new here and needing some help on updating UIView background with timers. Is it possible to have timers set within a specific hour range? I'm wanting to be able to have it where the background will change colors depending on what time of day it is. Also, could I have it to where it will update even if an user is not using the app? I want my app to be able to have really good UI and user experience without having to stay on the app. I have been researching timers, however they are using seconds, that's why I'm curios into if I can use hours instead. Thank you in advance!

2

There are 2 best solutions below

6
Andrew Romanov On BEST ANSWER

I think you have two different tasks:

  1. Select appropriate view's style based on current time. (I think it is simple, just get local time in viewWillAppear or in willMoveToWindow with NSDate).
  2. Update the active view via changing of time.

To solve second task you can use timers. Just use timer to check time, for that you can schedule timer to nearest time where you should change the style:

let timer = Timer(fire: Date(timeIntervalSinceNow: 1000.0), interval: 60.0 * 60.0, repeats:true) { (sender) in
            //check current time and update view's background;
            //or send notification about time
        }
RunLoop.current.add(timer, forMode: .common)

To make your code better, create some class that will incapsulate timer creation and posting notification about time changing.

UPD
I think you should start from the begin. First, read about UIViewController, UIView, NotificationCenter, Timer. Also you should read about a event-driven architecture. After that you will look at your application and will decide where you can add the colorUpdate func.
If you need complete solution without knowledge for your application you can find someone who can create it for you (On Freelance or Upwork or on in some other place where you can find outsourcers).

P.S.
Adding the colorUpdate func into NotificationCenter is a bad idea, you can add this method in to a handler of a Notification.

10
Nikunj Kumbhani On

Working code for Swift 4.

For this simple thing can't need to use a timer or anything much code you can do it with UserDefaults

Add this in viewWillAppear method.

import UIKit

class MainVC: UIViewController {

    override func viewWillAppear(_ animated: Bool) {

        if let Last_Update_Date = kUserDefults_("last_update_date") as? Date{

            print(Date().minutes(from: Last_Update_Date)) // Time Different in minutes

            if Date().minutes(from: Last_Update_Date) > 60{
                // update view's background
                self.view.backgroundColor = UIColor.red
                // update date in UserDefaults
                kUserDefults(Date() as AnyObject, key: "last_update_date")
            }
        }else{

            kUserDefults(Date() as AnyObject, key: "last_update_date")
        }
    }
}

Store Value in UserDefaults

func kUserDefults(_ value : AnyObject, key : String ){
    let defults = UserDefaults.standard
    defults.setValue(value, forKey: key)
    defults.synchronize()
}

Retrieve Value from UserDefaults

func kUserDefults_( _ key : String) -> AnyObject? {
    let defults = UserDefaults.standard
    return defults.value(forKey: key ) as AnyObject?
}

Date extension

extension Date {
    /// Returns the amount of minutes from another date
    func minutes(from date: Date) -> Int {
        return Calendar.current.dateComponents([.minute], from: date, to: self).minute ?? 0
    }
}