Brandon
Brandon

Reputation: 1751

Looping UIImage Fade Transition with Multiple Images

I have a viewController where a UIImageView transitions between different images every 10 seconds. This works well until I try to add more than two images. I'm not sure what I'm doing wrong here, but I get the error: "No known class method for selector 'imageNamed::'

These are my methods:

viewDidLoad with a timer:

- (void)viewDidLoad {
    i = 0;
    [NSTimer scheduledTimerWithTimeInterval:10.0 target:self 
    selector:@selector(swapImage) userInfo:nil repeats:YES];
}

Here is the code that works with two images:

- (void)swapImage {

    background.image = [UIImage imageNamed:(i % 2) ? @"authBg1.png" : @"authBg2.png"];

    CATransition *transition = [CATransition animation];
    transition.duration = 1.0f;
    transition.timingFunction = [CAMediaTimingFunction 
    functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionFade;

    [background.layer addAnimation:transition forKey:nil];

    i++;
}

If I change the first background.image line to add a new image, I get the error:

background.image = [UIImage imageNamed:(i % 3) ? @"authBg1.png" : @"authBg2.png" : 
@"authBg3.png"];

Any ideas as to why this might not be working? Thanks!

Upvotes: 1

Views: 604

Answers (1)

meda
meda

Reputation: 45490

Ternary operator can only support 2 conditions unfortunately.

If you need more then 2 conditions use if statements.


If/else

if(i%2 == 0){

}elseif(i%3==0){

}else{

}

But if you really want ternary you can nest them though its harder to read

background.image = [UIImage imageNamed:(i % 2) ? @"authBg1.png" : (i % 3) ?@"authBg3.png" : @"authBg2.png"];

Upvotes: 2

Related Questions