1

Here is my code of slide show of images:

-(void)setImage
{
     imageView.image = [UIImage imageNamed:[arrImages objectAtIndex:0]];
     [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
}

-(void)changeImage
{

   imageId = imageId + 1;

   int id = imageId % [arrImages count];
   CATransition * trs = [CATransition animation];
   trs.duration = 0.5;
   [trs setType:kCATransitionPush];
   imageView.image = [UIImage imageNamed:[arrImages objectAtIndex:id]];
   [imageView.layer addAnimation:trs forKey:kCATransition];
}

i am calling setImage() method on button click.

first time when i am clicking on button it works fine but after that whenever i click on button, speed of slideshow increases.

Can any one tell me why this happens.

Thanx.

Bhagyashree mahajan
  • 531
  • 1
  • 6
  • 14

1 Answers1

1

First take globally a timer.

 NSTimer *timer;

 -(void)setImage
 {
    imageView.image = [UIImage imageNamed:[arrImages objectAtIndex:0]];
    if (timer)
    {
      [timer invalidate];
      timer = nil;
    }

    timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
 }

Try this one :)

Soumya Ranjan
  • 4,817
  • 2
  • 26
  • 51