【问题标题】:Endless runner imageview animation (No SpriteKit)无尽的跑步者图像视图动画(没有 SpriteKit)
【发布时间】:2016-07-21 11:08:26
【问题描述】:

有没有一种方法可以在不使用 CABasicAnimation 的情况下从右到左为 UIImageView 设置动画?我遇到了麻烦,我想找到另一种方法。

“地面”图像的大小为 1200(宽度)x 33(高度),我想将其从右向左移动,以模拟无限重复的无尽跑步者。

没有 CALayer/CABasicAnimation 怎么实现?

谢谢!

【问题讨论】:

  • 你的意思是移动 UIImageView?
  • 实际上是一种无需使用 CABasicAnimation 即可模拟无尽跑道的方法。我在 UIImageView 中有资产。
  • 所以你有几张图片要在一个 UIImageView 中使用,或者你只有一张图片,你需要移动 UIImageView 以达到预期的效果?
  • 只有一张我想重复使用的图片并附加到前一张图片的末尾
  • 我明白了。这很容易做到

标签: ios objective-c uikit calayer cabasicanimation


【解决方案1】:

UIView 上有很多方法允许任何动画,尤其是移动视图。

你可以像这样移动你的 imageView:

[UIView animateWithDuration:10
                 animations:^{

                     yourImageView.frame = frame_you_need;
                 }
                 completion:^(BOOL finished){

                    //start from the very beginning
                    //set initial frame to the image view and run the 
                    // animation again
                 }];

所以你需要两个图像视图。设置它们的开始位置,在两者上运行动画,在动画完成时再次重复动画。就是这样

更新

我决定自己实现这个功能。这是我的动画函数的外观:

- (void)runView:(UIView *)aView withStartPosition:(CGPoint)startPosition
{
aView.center = startPosition;

[UIView animateWithDuration:3
                      delay:0
                    options:UIViewAnimationOptionCurveLinear
                 animations:^{

                     CGPoint newPosition = CGPointMake(aView.center.x + aView.frame.size.width, aView.center.y);
                     aView.center = newPosition;
                 }
                 completion:^(BOOL finished){

                     [self runView:aView withStartPosition:startPosition];

                 }];
}

演示此功能的项目在这里:Github

【讨论】:

  • 太棒了! GitHub 上的项目对我帮助很大,如果我能看到它们,我会更好地理解:) 谢谢!
【解决方案2】:

由于 UIView.animateWithDuration 有时难以实现,我创建了一种执行动画的方法,序列和组更容易。你可以在这里查看:GitHub - UIAnimation .这是 SKAction 的 UIKit 版本。

使用此 UIAnimation 实现您想要的方法是:

//imgView is your UIImageView
let positioning = UIAnimation.moveTo(CGPointMake(view.frame.width+imgView.frame.width/2,view.frame.height/2), duration: 0) //put the UIView on the right side of the screen
let movement = UIAnimation.moveTo(CGPointMake(-imgView.frame.width/2),view.frame.height/2), duration: 5) //moves the UIView to the left side of the screen
let animation = UIAnimation.sequence([positioning,movement]) //do them sequentially
let foreverAnimation = UIAnimation.repeatAnimationForever(animation) //repeat the sequence forever
imgView.runAnimation(foreverAnimation) //start the animation

这里有趣的是,您可以将“foreverAnimation”存储在一个变量中,并将其重用于您想要移动的所有图像视图。您只需要调用 runAnimation。

希望对您有所帮助。如果您对脚本有任何问题或建议,请与我联系 =)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-24
  • 2022-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-30
  • 1970-01-01
相关资源
最近更新 更多