连续向左移动图像

时间:2014-03-05 23:39:46

标签: ios animation nstimer

我需要连续从左到右运行这个动画。现在正在工作,但只显示1次(加载游戏时)。我想让这个动画连续不断地从左到右运行。定时器和速度已经正常工作。这是我目前的代码:

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

有什么建议吗?感谢

2 个答案:

答案 0 :(得分:4)

您可以使用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];

答案 1 :(得分:0)

如果您希望飞机在从侧面消失后在起始侧出现,只需在通过窗口框架后重置开始侧的中心关闭屏幕。如果你想要另一架飞机出现并在飞机离开之后开始飞行,那就添加另一架飞机并同时移动它们。

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