简体   繁体   English

连续从左向右移动图像

[英]Move image left to right continuously

I need to make run this animation from left to right continuously. 我需要使该动画从左到右连续运行。 Now Is working, but show 1 time only (when the game is loaded). 现在正在工作,但仅显示1次(加载游戏时)。 I want to make this animation run continously, left to right continuously. 我想使该动画连续地,从左到右连续地运行。 Timer and speed is already working. 计时器和速度已经在起作用。 Here my current code: 这是我当前的代码:

- (void)airplane1Code {
    airplane1.center = CGPointMake(airplane1.center.x + 10, airplane1.center.y);
}

Any suggestion? 有什么建议吗? Thanks 谢谢

You can create a UIView animation with the UIViewAnimationOptionRepeat 您可以使用UIViewAnimationOptionRepeat创建UIView动画

airplane1.center = CGPointMake(startX, startY);
[UIView animateWithDuration:10.0 //10seconds
                      delay: 0
                    options: UIViewAnimationOptionRepeat
                 animations:^{
                     airplane1.center = CGPointMake(endX, endY);
                 }
                 completion:nil];

That will make it do the same animation from start to end over and over. 这将使它从头到尾一遍又一遍地执行相同的动画。 In order to stop the animation, call 为了停止动画,请致电

[airplane1.layer removeAllAnimations];

If you want the airplane to show up on the start side after it disappears from the end side, just reset the center off screen on the start side once it passes the window's frame. 如果您想让飞机从端侧消失后在端侧出现,只需在飞机通过窗口框后将其中心位置重置为离开端侧的屏幕即可。 If you want another plane to show up and start flying after that one has left, then add another plane object and move them at the same time. 如果要让另一架飞机出现并在离开后开始飞行,则添加另一架飞机对象并同时移动它们。

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;

    -(void) airplane1Code{
        airplane1.center = CGPointMake(airplane1.center.x + 10, airplane1.center.y);
        if(airplane1.center > screenWidth + halfTheImageSize)
           // if the plane is completely off the screen, move the image to other side
           // and keep running this method with your loop
           airplane1.center = CGPointMake(0 - halfTheImageSize, airplane1.center.y);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM