2

我为我的应用制作了一个简单的照片幻灯片,问题是我的代码在最后一张图像之后不再重复图像!这是我的代码:

- (IBAction)start:(id)sender {

    _button.hidden = YES;
    [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(photoCounter) userInfo:nil repeats:YES];


}


- (void)photoCounter {

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.90];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:NO];
    [self updatePhoto];
    imageCount ++;
    [UIView commitAnimations];

}

- (void)updatePhoto {

    switch (imageCount) {

        case 0:
            _images.image = [UIImage imageNamed:@"wall1.jpg"];
            break;

        case 1:
            _images.image = [UIImage imageNamed:@"wall2.jpg"];
            break;
        case 2:
            _images.image = [UIImage imageNamed:@"wall3.jpg"];
            break;
        case 3:
            _images.image = [UIImage imageNamed:@"wall4.jpg"];
            break;
        case 4:
            _images.image = [UIImage imageNamed:@"wall5.jpg"];
            break;

        default:
            break;
    }

}
4

3 回答 3

5

从 photoCounter 方法中删除 imageCount++ 和

- (void)updatePhoto 
{
    switch (imageCount) 
    {
        case 0:
            _images.image = [UIImage imageNamed:@"wall1.jpg"];
            break;
        case 1:
            _images.image = [UIImage imageNamed:@"wall2.jpg"];
            break;
        case 2:
            _images.image = [UIImage imageNamed:@"wall3.jpg"];
            break;
        case 3:
            _images.image = [UIImage imageNamed:@"wall4.jpg"];
            break;
        case 4:
            _images.image = [UIImage imageNamed:@"wall5.jpg"];
            break;

        default:
            break;
    }
    imageCount ++;
    if (imageCount > 4)
        imageCount = 0;

}
于 2013-03-29T08:56:45.580 回答
2

可能更好

- (void)updatePhoto {
    NSString *imageName = [NSString stringWithFormat:@"wall%d", imageCount];
    _images.image = [UIImage imageNamed:imageName];
    (imageCount > 4) ? imageCount = 0 : imageCount++;

}

于 2013-03-29T10:14:13.387 回答
1

在函数- (void)photoCounter... 而不是imageCount++,尝试把

if(imageCount > 4){ 
    imageCount = 0;
} else { 
    imageCount++;
}
于 2013-03-29T08:52:10.963 回答