2012-04-24 79 views
3

我有以下代碼的有線問題。在iphone上幻燈片和轉換?

如果我使用這些代碼,並在模擬器上運行它,他只會向我顯示陣列中的前兩張圖片。如果我在Iphone上運行它,他會向我顯示第一張照片。 也他不重複的圖片.-

什麼iam做錯了?

- (void)viewDidLoad{ 
    [super viewDidLoad]; 

    welcomePhoto = [NSArray arrayWithObjects:@"Bild.jpeg",@"Bild1.jpeg", @"Bild2.png", nil]; 
    imageView.image = [UIImage imageNamed:[welcomePhoto objectAtIndex:0]]; 
    [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(transitionPhotos) userInfo:nil repeats:YES]; 
} 


-(void)transitionPhotos{ 
    int photoCount; 
    if (photoCount < [welcomePhoto count] - 1){ 
     photoCount ++; 
    }else{ 
     photoCount = 0; 
    } 
    [UIView transitionWithView:self.imageView 
         duration:2.0 
         options:UIViewAnimationOptionTransitionCrossDissolve 
        animations:^{ imageView.image = [UIImage imageNamed:[welcomePhoto objectAtIndex:photoCount]]; } 
        completion:NULL];  
} 
+0

您是否嘗試過使用調試器來逐步通過您的代碼? – Stefan 2012-04-24 14:00:09

回答

3

嘗試在viewDidLoad中添加此項。

UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 90, 100, 100)]; 
[self.view addSubview:animatedImageView]; 
animatedImageView.animationImages = [NSArray arrayWithObjects:  
            [UIImage imageNamed:@"bild.png"], 
            [UIImage imageNamed:@"bild.png"], 
            [UIImage imageNamed:@"bild.png"], nil]; //etc 
animatedImageView.animationDuration = 3.0f * [animatedImageView.animationImages count]; 
animatedImageView.animationRepeatCount = 0; 
[animatedImageView startAnimating]; 
[self.view addSubview: animatedImageView]; 
+0

thx你的代碼正在工作。如何添加轉換到您的代碼?有沒有像我這樣的其他方式? – Bashud 2012-04-24 14:12:26

1

我找到了。您應該跳過初始化transitionChotos中的photoCount。把它變成一個iVar或財產。

+0

Ahhhhhh thx。那是我的錯 – Bashud 2012-04-24 14:55:28

+0

varsågod!我猜 – TompaLompa 2012-04-24 14:59:22

2

試試這個代碼

NSArray *imagesArray; 
int photoCount; 
UIImageView *imageView; 
-(void)setupImageView{ 
    photoCount = 0; 
    [[NSArray alloc]initWithObjects:[UIImage imageNamed:@"imgA.png"] ,[UIImage imageNamed:@"imgB.png"] ,[UIImage imageNamed:@"imgC.png"] ,[UIImage imageNamed:@"imgD.png"] ,[UIImage imageNamed:@"imgE.png"] , nil];   imageView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, height)]; 
    imageView.image = [imagesArray objectAtIndex:photoCount]; 
    [self.view addSubview:imageView]; 
    [NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:@selector(transitionPhotos) userInfo:nil repeats:YES]; 

} 

-(void)transitionPhotos{ 


if (photoCount < [imagesArray count] - 1){ 
    photoCount ++; 
}else{ 
    photoCount = 0; 
} 
[UIView transitionWithView:self.imageView1 
        duration:2.0 
        options:UIViewAnimationOptionTransitionCrossDissolve 
       animations:^{ imageView.image = [imagesArray objectAtIndex:photoCount]; } 
       completion:NULL];  
}