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

最佳答案

不熟悉这两个应用程序(是的,游戏Ludite),但是如果视图的宽度大于屏幕,则可以移动它的位置/中心/框架/变换/等。要使其移动,您也可以为该动作设置动画。

如果您将视图宽度的前320个点(我们以点为单位,而不是像素)与后320个点相等,则可以在视图到达终点时跳过其位置,并永远继续下去。



编辑(示例代码):

- (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];
}

关于ios - 菜单中的动画,例如“愤怒的小鸟”或“小翅膀”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6780824/

10-10 04:02