2010-04-18 136 views
0

我正在創建我的第一個iPhone本機應用程序(初學者Objective-C)。在我的主菜單標題屏幕上,我想讓背景圖像慢慢向左移動。是否可以平鋪背景圖像並移動它?我基本上試圖讓我的雲在整個背景中漂移。你會如何做到這一點?移動iPhone應用程序背景圖像

回答

0

我覺得你可以用UIScrollView做點聰明的事情。視圖加載後,您可以在後臺循環中爲視圖內容偏移添加動畫。查看ScrollViewSuite示例代碼,獲取關於滾動視圖和平鋪的一些好的提示。希望有所幫助。

2

我會添加一個UIImageView到你的屏幕(放在屏幕上的其他所有東西下)。將雲圖像放在UIImageView中,並將幀值設置爲在整個屏幕上移動。

像這樣的東西(此代碼是未經試驗)將動畫圖像視圖,向左移動在屏幕上:

cloudsImage.frame = CGRectMake(0,0,320,100); // depends on your image 

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:3.0f]; // duration affects how fast it moves 

cloudsImage.frame = CGRectMake(-320,0,320,100); // moves left off the screen 

[UIView commitAnimations]; 

你也可以設置動畫相同的圖像,並從右側的同時在滑動第一個時間從左邊走出來,使它看起來像來自右邊的連續的雲層流。

cloudsImage1.frame = CGRectMake(0,0,320,100); // depends on your image 
cloudsImage2.frame = CGRectMake(320,0,320,100); // start off the right edge 

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:3.0f]; // duration affects how fast it moves 

cloudsImage1.frame = CGRectMake(-320,0,320,100); // moves left off the screen 
cloudsImage2.frame = CGRectMake(0,0,320,100);  // moves left onto the screen 

[UIView commitAnimations]; 
0

我做了類似的基於塊的動畫來實現這種效果。每個動畫塊都在一個單獨的函數中定義。

假設兩張圖像都與屏幕大小相同。

一個圖像在屏幕上完全啓動,一個圖像從屏幕左側開始,其右側與屏幕的左側邊界齊平(即,它完全偏離屏幕左側)。 讓我們調用屏幕上的圖像'A'和屏幕外圖像'B'。

function1() 第一個塊將兩個圖像的屏幕寬度向右移動 完成後,它將剛剛移出屏幕('A')的圖像設置回屏幕左側,然後調用function2()。

函數2() 第二塊移動兩個圖像屏幕寬度向右 完成時它設置在圖像剛移出屏幕(「B」)返回到左畫面,然後調用功能1()。

這裏的第一個功能:

-(void)animate_1 
{ 
[UIView animateWithDuration:roadTimerPeriod delay:0.0 
    options:UIViewAnimationOptionCurveLinear|UIViewAnimationOptionAllowUserInteraction 
    animations: ^{ 
    // frame 1 moves off bottom 
imageAnimatedRoad1.transform = CGAffineTransformTranslate(imageAnimatedRoad1.transform,0,480); 
// frame 2 moves onto top 
imageAnimatedRoad2.transform = CGAffineTransformTranslate(imageAnimatedRoad1.transform,0,480);  
      } 
completion:^(BOOL finished) 
      { 
      // reset frame 1 ready to move on 
      imageAnimatedRoad1.frame = CGRectMake(0,-480,320,480); 
      [self animate_2]; 
      } ]; 
} 

你認爲這是一個很好的解決方案,或者有我錯過了什麼昭然若揭?