0

With the help of a few threads here I created a slideshow that starts as soon as my view loads However I am trying to make a slight modification where, I want the images to be random Not showing in the order in the array or in any particular order. This is my codde

-(void)viewDidAppear:(BOOL)animated
{
NSArray *myImages=[NSArray arrayWithObjects:
                   [UIImage imageNamed:@"Pitt Bull"],
                   [UIImage imageNamed:@"German Sherpard"],
                   [UIImage imageNamed:@"Pincer"],
                   nil];

int indexed=arc4random()%[myImages count];
UIImage *image=[myImages objectAtIndex:indexed];
NSArray *imageAr=[NSArray arrayWithObject:image];

[NSTimer scheduledTimerWithTimeInterval:12.0
                                        target:self
                                         selector:@selector(viewDidAppear:)
                                         userInfo:Nil
                                         repeats:YES];

[self.kenView animageWithImages:imageAr
                               transitionDuration:12
                               loop:YES
                              isLandscape:YES];

}

However it only works once. it loads a random picture at the start, but after that it keeps loading the same image. I can see that is because indexed is only assigned an integer when the view "did appear" . I was wondering if there was another way too select a new random number since I am not using a button/action as I saw many of other members doing. OR a better way of accomplishing this.

Thank you

Jennifer
  • 89
  • 7

2 Answers2

0

You may want to try this function, + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats from NSTimer.

For example, in ViewDidLoad

timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(showImage:) userInfo:nil repeats:YES];

and declare a function

- (void)showImage:(NSTimer*)timer
{
    // your code to generate random index and animate image transition
}

if for some reason, you want to stop the timer

[timer invalidate];

Update

To avoid the timer messing up issue, trigger timer inside showImage, in ViewDidLoad, do [self showImage]

- (void)showImage
{
    // your code to generate random index and animate image transition
    // trigger the timer when the transition is finished
    // You can leverage the method animateWithDuration:animations:completion:
    // from UIView
    [UIView animateWithDuration:12
        animations:^(){
            // image transition
        }
        completion:^(BOOL finished){
          timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(showImage) userInfo:nil repeats:NO];
    }]
}

This should provide you the idea.

Dino Tw
  • 3,167
  • 4
  • 34
  • 48
  • This works fine. However, the method I am using already comes with a duration timmer and I think it is messing with the timer. Cause sometimes the image is displayed for 12 second then the next one changes after 2 seconds – Jennifer Jun 03 '14 at 19:26
  • @Jennifer, please see the update to see if it helps. – Dino Tw Jun 03 '14 at 22:41
  • still having some troubles.where you commented //image transition. Im not exactly sure I can add my [self.kenView animageWithImages:imageAr etc etc method there, because it also has a timmer – Jennifer Jun 04 '14 at 01:37
  • The reason I use `UIView animateWithDuration:` is because it has the completion block. I don't know how your `view animageWithImages:` is implemented. If it also has completion block, you can just use it. If not, then copy its image transition code to where `//image transition` is, it should does the trick. – Dino Tw Jun 04 '14 at 06:19
0

Consider using this method to randomly shuffle the elements of your array at startup.

Community
  • 1
  • 1
i_am_jorf
  • 53,608
  • 15
  • 131
  • 222