我为我的应用做了一个简单的照片幻灯片,问题是我的代码在最后一张图片后不再重复图像了!这是我的代码:
- (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;
}
}
答案 0 :(得分: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;
}
答案 1 :(得分:2)
也许更好
- (void)updatePhoto {
NSString *imageName = [NSString stringWithFormat:@"wall%d", imageCount];
_images.image = [UIImage imageNamed:imageName];
(imageCount > 4) ? imageCount = 0 : imageCount++;
}
答案 2 :(得分:1)
在函数- (void)photoCounter
...而不是imageCount++
中,尝试添加
if(imageCount > 4){
imageCount = 0;
} else {
imageCount++;
}