Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Jonas Gamburg
Jonas Gamburg
11,193 Points

Background Wallpaper changes with fade animation every few seconds

Greeting m'ladies and m'lords, please be so kind and distribute an answer for me humble question: So I have a reminders app, the main screen has a simple login interface (textfield 2x, label, login button etc) The background is my custom choice (made it with self.view.backgroundColor = [... ...];) And I want to make that on this background there will be constantly a few images that'll change (like in a loop) and each image will fade out and a new image will fade in.

2 Answers

Heres what I would do:

Store the names of my images in an array. Use a NSTimer to change the background every N number of seconds. Then use an UIView animation to change the background

NSArray * allImages = [[NSArray alloc]initWithObjects:@"image1.png",@"image2.png",@"image3.png", nil];
NStimer * animationTimer = [NSTimer scheduledTimerWithTimeInterval:2.0
                                                          target:self
                                                        selector:@selector(changeBackground)
                                                        userInfo:nil
                                                         repeats:YES];  //make sure repeats is set to YES
-(void) changeBackground{

[UIView animateWithDuration:0.8
                     animations:^{

                     //Your code:
                     //use [allImages objectAtIndex:] and just choose a random index so a different image is shown.
                     self.view.backgroundColor = [... ...]; // set the background using UIImageNamed

                     }];
}