2010-04-18 137 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]; 
      } ]; 
} 

你认为这是一个很好的解决方案,或者有我错过了什么昭然若揭?