Ios 连续从左向右移动图像

Ios 连续从左向右移动图像,ios,animation,nstimer,Ios,Animation,Nstimer,我需要让这个动画从左到右连续运行。现在正在工作,但仅显示1次(当游戏加载时)。我想让这个动画从左到右连续运行。计时器和速度已经开始工作。以下是我当前的代码: - (void)airplane1Code { airplane1.center = CGPointMake(airplane1.center.x + 10, airplane1.center.y); } 有什么建议吗?谢谢如果你想让飞机在从末端消失后出现在起始端,只要在飞机通过窗口框架后重置起始端的中心离屏即可。如果希望另一个飞

我需要让这个动画从左到右连续运行。现在正在工作,但仅显示1次(当游戏加载时)。我想让这个动画从左到右连续运行。计时器和速度已经开始工作。以下是我当前的代码:

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

有什么建议吗?谢谢

如果你想让飞机在从末端消失后出现在起始端,只要在飞机通过窗口框架后重置起始端的中心离屏即可。如果希望另一个飞机出现并在该飞机离开后开始飞行,请添加另一个飞机对象并同时移动它们

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);
    }

如果你想让飞机在从末端消失后出现在起始端,只要在飞机通过窗口的框架后重置起始端的中心离屏即可。如果希望另一个飞机出现并在该飞机离开后开始飞行,请添加另一个飞机对象并同时移动它们

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);
    }

可以使用UIViewAnimationOptionRepeat创建UIView动画

airplane1.center = CGPointMake(startX, startY);
[UIView animateWithDuration:10.0 //10seconds
                      delay: 0
                    options: UIViewAnimationOptionRepeat
                 animations:^{
                     airplane1.center = CGPointMake(endX, endY);
                 }
                 completion:nil];
这将使它从开始到结束一遍又一遍地做相同的动画。要停止动画,请调用

[airplane1.layer removeAllAnimations];

可以使用UIViewAnimationOptionRepeat创建UIView动画

airplane1.center = CGPointMake(startX, startY);
[UIView animateWithDuration:10.0 //10seconds
                      delay: 0
                    options: UIViewAnimationOptionRepeat
                 animations:^{
                     airplane1.center = CGPointMake(endX, endY);
                 }
                 completion:nil];
这将使它从开始到结束一遍又一遍地做相同的动画。要停止动画,请调用

[airplane1.layer removeAllAnimations];

@HEMAN85正确的方法是Cameron建议的。但您可能需要一个不同的动画(例如,可能是具有在循环中前后移动的关键帧的动画)。查阅Cameron答案中的方法文档,了解其工作原理。@HEMAN85正确的方法是Cameron建议的方法。但您可能需要一个不同的动画(例如,可能是具有在循环中前后移动的关键帧的动画)。查阅Cameron答案中的方法文档,了解它是如何工作的。我想我们已经接近了,但这段代码不起作用。我收到一个错误“不知道选择器的类方法”animateWithDuration:delay:option::“啊,我的错-这是一个打字错误。我在另一台机器上从屏幕上读到的。在动画消息的末尾添加一个
completion:^{}
参数。我在回答中修复了示例,但是使用了nil来完成,因为您将让它无限期地运行。如果您开始在右括号前键入“completion”,Xcode将自动完成。不走运,这里是我当前的代码:airplane1.center=CGPointMake(+200,50);[UIView animateWithDuration:3.0//10秒延迟:0选项:UIView animationoption重复动画:^{airplane1.center=CGPointMake(+330,80);}完成:无];怎么了?我在-viewdide中复制/粘贴了您的代码,并且工作正常。如果尝试在-viewWillDisplay中启动动画,动画可能会变得不稳定,因此在-viewDidLoad或-viewWillDisplay中进行初始定位并在中启动动画-viewDidAppear@HEMAN85正如阿比·贝克特所证实的那样,这个答案是正确的。由于我/我们花时间帮助您,您能将其标记为已接受吗?谢谢我想我们很接近了,但是这个代码不起作用。我收到一个错误“不知道选择器的类方法”animateWithDuration:delay:option::“啊,我的错-这是一个打字错误。我在另一台机器上从屏幕上读到的。在动画消息的末尾添加一个
completion:^{}
参数。我在回答中修复了示例,但是使用了nil来完成,因为您将让它无限期地运行。如果您开始在右括号前键入“completion”,Xcode将自动完成。不走运,这里是我当前的代码:airplane1.center=CGPointMake(+200,50);[UIView animateWithDuration:3.0//10秒延迟:0选项:UIView animationoption重复动画:^{airplane1.center=CGPointMake(+330,80);}完成:无];怎么了?我在-viewdide中复制/粘贴了您的代码,并且工作正常。如果尝试在-viewWillDisplay中启动动画,动画可能会变得不稳定,因此在-viewDidLoad或-viewWillDisplay中进行初始定位并在中启动动画-viewDidAppear@HEMAN85正如阿比·贝克特所证实的那样,这个答案是正确的。由于我/我们花时间帮助您,您能将其标记为已接受吗?谢谢