无尽的赛跑者 imageview 动画(无 SpriteKit)

Endless runner imageview animation (No SpriteKit)

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

"ground" 图像的尺寸为 1200(宽)x 33(高),我想将它从右向左移动,以模拟无限重复的无休止跑步者。

没有CALayer/CABasicAnimation如何实现?

谢谢!

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

由于 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' 存储在一个变量中,并将其重新用于您要移动的所有 imageView。你只需要调用 runAnimation.

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