1

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!

ZWDesigns
  • 23
  • 5

2 Answers2

0

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.

Andrew Romanov
  • 4,774
  • 3
  • 25
  • 40
  • Could I update the background color inside the timer? I'll have six color changes. One will last eleven hours. – ZWDesigns Oct 25 '18 at 04:40
  • I think better solution create method updateColor, and call this method from willAppear/willMoveToWindow and from a timer's callback. But if you have many views which you should update, use NotificationCenter and one timer (you can create it in AppDelegate) to notify views about time changing – Andrew Romanov Oct 25 '18 at 05:00
  • Would it be possible to use the one timer to hold an array of times and use Notification Observer to instantiate it, then use it with my updateColor method? Sorry this is all new to me so I'm really wanting to achieve my idea. – ZWDesigns Oct 25 '18 at 05:12
  • @ZWDesigns Please check my answer it will resolve your issue. – Nikunj Kumbhani Oct 25 '18 at 05:53
  • Hey Andrew, would I pass my colorUpdate func into the NotificationCenter? – ZWDesigns Oct 29 '18 at 16:14
  • Got it working with a timer, thanks for your help @AndrewRomanov! – ZWDesigns Oct 31 '18 at 05:31
0

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
    }
}
Nikunj Kumbhani
  • 3,758
  • 2
  • 26
  • 51
  • Could I have it to where it will change at certain times like 6am, 9am, 12pm, etc.? In if Date().minutes(from: Last_Update_Date) > 60? – ZWDesigns Oct 25 '18 at 15:24
  • Also I'm doing everything programmatically, so how could I set this in my UIView constraints so it'll update? – ZWDesigns Oct 25 '18 at 17:36
  • @ZWDesigns it always returns time different from your last open that view controller either application is open or not and you need to add this in viewWillAppear I have already mentioned this, For testing Purpose check with 5 minutes different **Date().minutes(from: Last_Update_Date) > 5** and then implement according to your requirement – Nikunj Kumbhani Oct 26 '18 at 04:21
  • Okay cool, and for my UIView, I have it in a constraint extension. Do I need to do something inside of my constraint func in order for the background to show up? Right now as is, it's not showing. – ZWDesigns Oct 26 '18 at 04:30
  • @ZWDesigns simply when you need to change your view color put this code that's it. – Nikunj Kumbhani Oct 26 '18 at 04:47
  • Could I send you my bitbucket project to see what I'm missing? – ZWDesigns Oct 26 '18 at 04:53
  • https://bitbucket.org/zcsmouse/my-fishing-buddy-ios/downloads/?tab=branches - I'm on mainvcbranch – ZWDesigns Oct 26 '18 at 04:58
  • @ZWDesigns sorry to say, Dear, I don't have time to review your code and my answer is completely working code from my current swift 4 project you need to check proper. – Nikunj Kumbhani Oct 26 '18 at 05:01
  • My UIView isn't showing up though :( I put everything like you said too – ZWDesigns Oct 26 '18 at 05:06
  • @ZWDesigns You only initialize **weatherBG** not add as a subview in View controller File either Add that view as a sub view and then use it or use **self.view.backgroundColor = UIColor.red** instant of weatherBG. Check my Updated Answer – Nikunj Kumbhani Oct 27 '18 at 04:26