2013-03-29 186 views
2

我爲我的應用程序做了一個簡單的照片幻燈片,問題是我的代碼不會在最後一張圖像後重復圖像!這裏是我的代碼:iOS - 創建照片幻燈片

- (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; 
    } 

} 

回答

5

刪除imageCount ++從photoCounter方法和

- (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

在功能- (void)photoCounter ...而不是imageCount++,嘗試把

if(imageCount > 4){ 
    imageCount = 0; 
} else { 
    imageCount++; 
} 
2

也許更好

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

}