-1

I can't figure out how to do this:

I have an ImageView and by clicking the the button I'd like in the ImageView to be shown: first image, then wait few seconds and do some while loop which will find the second image (by following instructions in the while loop from many other images), and then show the chosen (second) image.

I'm not sure I've described my issue very clear so here is sample code:

- (IBAction)button:(id)sender {
  i=0;
  UIImage *img = [UIImage imageNamed:@"image1.png"];
                 [first setImage:img];
  /*some code which sets the time the image1 stay shown*/
  while (i<some_variable) {
  /*blah blah blah*/
  if (something) {UIImage *img = [UIImage imageNamed:@"image2.png"];
                 [first setImage:img];}
  if (something else) {UIImage *img = [UIImage imageNamed:@"image3.png"];
                 [first setImage:img];}
  ...
  /*code code code*/
  i++;
  }

}

This one hasn't solve my problem:

[NSThread sleepForTimeInterval:1.0];

it just wait's blank then process the code and show the chosen image.

This should be something really basic and I can't think of any idea.

4

1 回答 1

0

This solved my issue:

- (IBAction)button:(id)sender {

  UIImage *img = [UIImage imageNamed:@"image1.png"];
                 [first setImage:img];

  double delayInSeconds = 1.0; //set delay time
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
  i=0;
  while (i<some_variable) {
  /*blah blah blah*/
  if (something) {UIImage *img = [UIImage imageNamed:@"image2.png"];
                 [first setImage:img];}
  if (something else) {UIImage *img = [UIImage imageNamed:@"image3.png"];
                 [first setImage:img];}
  ...
  /*code code code*/
  i++;
  }
  });
}
于 2013-10-06T17:28:58.137 回答