Tanner Quigley Tanner Quigley - 12 days ago 7
iOS Question

Simple UIImageView Slideshow IOS timer issue

Im attempting to create a simple slideshow where the image of a UIImageView changes every three seconds. First I call

[self slideShow]
in my
viewDidLoad
. The Timer I have only runs once for some reason and I can't figure it out. Here are my other methods:

- (void)slideShow
{
timer = [NSTimer timerWithTimeInterval:3.0
target:self
selector:@selector(imChange)
userInfo:nil
repeats:YES];
[timer fire];
}

- (void)imChange
{
NSLog(@"%d",slideNum);
switch (slideNum) {
case 0:
imView.image = img1;
break;

case 1:
imView.image = img2;
break;

default:
break;
}

slideNum++;

if (slideNum > 1) {
slideNum = 0;
}
}


Edit: Timer is an instance variable

Answer

use this implementation -

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

because with timerWithTimeInterval parameter you should add your timer in NSRunLoop manually, like this:

[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
Comments