2012-01-03 57 views
0

如何在一個視圖中一個接一個地顯示兩個圖像,我有兩個圖像命名爲image1和image2,當用戶打開一個特定的頁面時,它想顯示image1,然後在2秒後image1散開並顯示image2,然後在2秒後image2 disappers並顯示image1並繼續。我希望你能理解我的問題。如何做到這一點。 在此先感謝。如何在一個視圖中一個接一個地顯示兩個圖像,反之亦然?

+0

請結算MadhavanRP答案。我猜它做形象東西的repetation完美的方式... – 2012-01-03 08:11:07

回答

2

你可以做到這一點

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

[imageViewObj setImage:[UIImage imageNamed:@"Image1.png"]]; 

NSTimer *ImgChangeTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 
                target:self 
               selector:@selector(changeImage) 
               userInfo:nil 
               repeats:YES]; 

} 

-(void)changeImage 
{ 
if (imageViewObj.image==[UIImage imageNamed:@"Image1.png"]) 
{ 
    [imageViewObj setImage:[UIImage imageNamed:@"Image2.png"]]; 

} 
else 
{ 
    [imageViewObj setImage:[UIImage imageNamed:@"Image2.png.png"]]; 
} 
} 
+0

什麼是imageViewObj? – stackiphone 2012-01-03 05:45:49

+0

imageViewObj是UIImageView類的對象。我通過XIB取得了UIImageView。 – Bonnie 2012-01-03 05:49:20

+0

它不工作,我把一個imageview在xib中,並創建imageViewObj作爲iboutlet名稱 – stackiphone 2012-01-03 06:08:58

0

你需要做的是 -

- (void)animateImages:(NSMutableArray *)ppls index:(int)i 
{ 
i += 1; 
[imageView1 setAlpha:0.0]; 
[imageView2 setAlpha:0.0]; 
[UIView animateWithDuration:0.5 
         delay:2 
        options:UIViewAnimationOptionCurveEaseIn 
       animations:^(void) 
{ 
    [imageView1 setAlpha:1.0]; 
} 
       completion:^(BOOL finished) 
{ 
    if(finished) 
    { 
     [UIView animateWithDuration:0.5 
           delay:0 
          options:UIViewAnimationOptionCurveLinear 
          animations:^(void) 
      { 
       [imageView1 setAlpha:0.0]; 
       [imageView2 setAlpha:1.0]; 
      } 
          completion:^(BOOL finished) 
      { 
       if(finished) 
       { 
        [self animateImages index:i]; 
       } 
      }]; 
    } 
}]; 
} 
+0

謝謝你的回覆,讓我chk。 – stackiphone 2012-01-03 05:32:58

0

您應該使用一個UIImageView來執行此類型的動畫。將其屬性animationImages設置爲您的圖像陣列。

myImageView.animationImages=[NSArray arrayWithObjects:image1,image2,nil]; 
myImageView.animationDuration=1;    //in seconds 
//myImageView.animationRepeatCount=0; 

animationRepeatCount默認爲0。您可以設置的次數,你希望動畫重複

相關問題