Make random image change every day

hello.

i want to make view with UIImageview that change random everyday at midnight.
the image’s are local on the app.
i try to do something but with out any success.

thanks for your help.

What are you using for date/time tracking?

Have you ever used NSDate before?

let currentDate = NSDate()

gives you the date = Sep 12, 2016 4:49PM. So get today’s date and compare it to a last date taken:

nowdate = NSDate()

if nowdate.compare(lastdate) == NSComparisonResult.OrderedDescending {
print(nowdate is later)
lastdate = nowdate
}

But this would have to be repeated quite often…you might be better off using NSTimeInterval:

NSTimeInterval timeInterval = 60 * 60 * 24 * 1; seconds in 1 day

and you can set it like this:

@IBAction func startTimer(sender: AnyObject) {
    timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval,
        target: self,
        selector: "whatToDoWhenTimerEnds:",
        userInfo: "Alert",
        repeats: false)
}

i try with only with NStimer.
don’t use NSDate.

this what i try

-(void)viewDidAppear:(BOOL)animated {

[super viewDidAppear:animated]; // Animation of sliding image

slideTransition = [CATransition animation];

CATransition * slideTransition; //instance variable

slideTransition.duration = 1.0;

slideTransition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

slideTransition.type = kCATransitionFade;

slideTransition.delegate = self;

// [NSTimer scheduledTimerWithTimeInterval:606024 target:self selector:@selector(changeImage:) userInfo:nil repeats:YES];
[NSTimer scheduledTimerWithTimeInterval:120 target:self selector:@selector(changeImage:) userInfo:nil repeats:YES];

}

-(void)changeImage:(NSTimer *)timer {

//change timer of the sling image and set the current page of page controller

slideTransition.subtype =kCATransitionFromRight; // or kCATransitionFromRight

[_Slidingimageview.layer addAnimation:slideTransition forKey:nil];

// [KlafImage .layer addAnimation:slideTransition forKey:nil];

index++;

if(index==[arrslidingimages count]){
    
    index=0; }

_Slidingimageview.image = [UIImage imageNamed:[arrslidingimages objectAtIndex:index]];

// KlafImage.image = [UIImage imageNamed:[arrslidingimages objectAtIndex:index]];

}

120 seconds? Ok, did it work for you?

You could use NSDateComponents to fire a NSNotification for you:

var calendar: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
var dateFire=NSDate()
// this next long line just specifies all the components we want to access
var fireComponents=calendar.components(NSCalendarUnit.CalendarUnitDay|NSCalendarUnit.CalendarUnitMonth|NSCalendarUnit.CalendarUnitYear|NSCalendarUnit.CalendarUnitHour|NSCalendarUnit.CalendarUnitMinute, fromDate:dateFire)
//Now if hour is > 1am, set next day's interval, which is 1 day from today (NSDate)
if (fireComponents.hour >= 1) {
    dateFire=dateFire.dateByAddingTimeInterval(86400)  // Use tomorrow's date

    fireComponents=calendar.components(NSCalendarUnit.CalendarUnitDay|NSCalendarUnit.CalendarUnitMonth|NSCalendarUnit.CalendarUnitYear|NSCalendarUnit.CalendarUnitHour|NSCalendarUnit.CalendarUnitMinute, fromDate:dateFire)
}


fireComponents.hour = 1
fireComponents.minute = 0

dateFire = calendar.dateFromComponents(fireComponents)!
// set notif
var localNotification = UILocalNotification()
localNotification.fireDate = dateFire
localNotification.alertBody = "Image will change"
localNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay

UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

i set 120 seconds just for testing.
i try what you wrote
thank you