菜单中的动画像愤怒的小鸟或小翅膀

时间:2011-07-21 18:26:11

标签: ios xcode ios4

我想知道如何在我的应用程序中添加滚动菜单,如小翅膀或愤怒的小鸟。在微小的翅膀中,背景从右向左移动,我想知道他们是如何完成的。提前谢谢。

1 个答案:

答案 0 :(得分:2)

不熟悉任何一个应用程序(是的,游戏Ludite),但是如果你创建一个比屏幕更宽的视图,你可以移动它的位置/中心/框架/转换/等。为了让它移动,你也可以为这个动作制作动画。

如果您将视图宽度的前320个点(我们以点,而不是像素测量)等于最后320个点,则可以在视图到达终点时跳转视图的位置并继续前进。 / p>


编辑(示例代码):

- (void)animateBannerLocation {
  UIView *view = _bannerView; // _bannerView.frame = CGRectMake(0,0,1000 ish,40);
  CGRect startRect = view.frame;
  CGRect destinationRect = view.frame;
  // assuming superview is width of screen
  destinationRect.origin.x = CGRectGetWidth(view.frame) - CGRectGetWidth(view.superview.frame);
  [UIView animateWithDuration:6.0 // time in seconds
                   animations:^(void) {
                     view.frame = destinationRect;
                   } completion:^(BOOL finished) {
                     /** if you want it to scroll forever:
                     view.frame = startRect;
                     [self animateBannerLocation];
                      **/
                   }];
}

未测试


编辑#2 也未经测试

- (void)viewDidLoad {
  [super viewDidLoad];
  UIImageView *_bannerView; /// - an ivar
  // not the best way to load an image you only use in one place:
  UIImage *image = [UIImage imageNamed:@"SomeBigBannerImage1000x40ish.png"];
  _bannerView = [[UIImageView alloc] initWithImage:image];
  [self.view addSubview:_bannerView];
}

编辑#3

审核了您的代码。您应该注意编译器警告。

- (void)animateBannerLocation {
  UIView *view = _bannerView; // _bannerView.frame = CGRectMake(0,0,1000 ish,40);
  CGRect startRect = view.frame;
  CGRect destinationRect = view.frame;
  // assuming superview is width of screen
  destinationRect.origin.x = - (CGRectGetWidth(view.frame) - CGRectGetWidth(view.superview.frame));
  [UIView animateWithDuration:6.0 
                        delay:0.0 
                      options:UIViewAnimationOptionCurveLinear
                   animations:^(void) {
                     view.frame = destinationRect;
                   } completion:^(BOOL finished) {
                     view.frame = startRect;
                     if (stopAnimation == NO)
                       [self animateBannerLocation];
                   }];

}


- (void)viewDidAppear:(BOOL)animated {
  [super viewDidAppear:animated];
  [self animateBannerLocation];
}