Uiview 如何无限播放连续动画?

Uiview 如何无限播放连续动画?,uiview,uianimation,Uiview,Uianimation,我有一系列的动画,应该是一个接一个的延迟触发。我使用的代码可以很好地工作,但是我希望我正在设置动画的所有视图都可以无限地在一个圆中设置动画 代码如下: [UIView animateWithDuration:2.0 delay:2.0 options: UIViewAnimationOptionTransitionNone animations:^{

我有一系列的动画,应该是一个接一个的延迟触发。我使用的代码可以很好地工作,但是我希望我正在设置动画的所有视图都可以无限地在一个圆中设置动画

代码如下:

[UIView animateWithDuration:2.0
                      delay:2.0
                    options: UIViewAnimationOptionTransitionNone
                 animations:^{
                     carouselTextView.frame=CGRectMake(-320, 200, 1600, 150);

                 }
                 completion:^(BOOL finished){
                      {
                         [UIView animateWithDuration:2.0
                                               delay:2.0
                                             options: UIViewAnimationOptionTransitionNone
                                          animations:^{
                                              carouselTextView.frame=CGRectMake(-640, 200, 1600, 150);

                                          }
                                          completion:^(BOOL finished){
                                               {
                                                  [UIView animateWithDuration:2.0
                                                                        delay:2.0
                                                                      options: UIViewAnimationOptionTransitionNone
                                                                   animations:^{
                                                                       carouselTextView.frame=CGRectMake(-960, 200, 1600, 150);

                                                                   }
                                                                   completion:^(BOOL finished){
                                                                       [UIView animateWithDuration:2.0
                                                                                             delay:2.0
                                                                                           options: UIViewAnimationOptionTransitionNone
                                                                                        animations:^{
                                                                                            carouselTextView.frame=CGRectMake(-1280, 200, 1600, 150);

                                                                                        }
                                                                                        completion:^(BOOL finished){

                                                                                        }];                                                                       }];
                                              };
                                          }];
                     };
                 }];

好的,那我就帮你。首先,您需要区分动画序列的可变部分和恒定部分。提供可变部件的某种数据存储,并使用一种方法对其进行循环

因此,看看代码,我所能看到的变化是:

carouselTextView.frame=CGRectMake(...);
(注意,所有这些代码都在类的实现文件中)

因此,让我们将它们存储在
静态
数组中(它是
静态
,因为我假设值永远不会更改,但是如果它们可以更改,那么它们将需要成为类的实例变量):

接下来,我们需要跟踪当前的carousel文本视图帧(指向
的索引\u carouselTextViewFrames[]
)。这必须是一个实例变量:

@interface YourClass ()
// private property
@property (nonatomic, assign) NSInteger carouselTextViewFrameIndex;
@end
并且必须将其初始化为
-1
,因此使用的第一个索引是
0
(请参见下面的
nextCarouselAnimation
):

现在,将启动下一个动画的方法:

- (void)nextCarouselAnimation {
    self.carouselTextViewFrameIndex = (self.carouselTextViewFrameIndex + 1) % NUM_CAROUSEL_TEXT_FIELD_FRAMES;

    [UIView animateWithDuration:2.0
                          delay:2.0
                        options:UIViewAnimationOptionTransitionNone
                     animations:^{
                         carouselTextView.frame = _carouselTextViewFrames[self.carouselTextViewFrameIndex];
                     }
                     completion:^(BOOL finished){
                         [self nextCarouselAnimation];
                     }];
}
最后,您只需要在适当的地方调用
nextCarouselAnimation
(您没有说您的类是从何而来的,所以我不知道它可能在哪里)来开始动画循环


注意:此代码未经测试,可能包含许多错误。

您不应该在完成部分从头开始重新编写所有动画,您需要做的只是再次运行该方法,代码如下:

completion:^(BOOL finished){
    [self methodYouNamed];
}

在完成部分编写代码,这里的语法很简单,它说**每当你的动画完成后,再次运行该方法,这不会结束,并且会一直持续下去**

@trojanfoe,这应该是有趣的吗?不,这不有趣;我相信你可以想象一些避免这种嵌入式风格的代码结构。@trojanfoe伙计来吧,我对编程和stackoverflow是新手。你要帮忙吗?
- (void)nextCarouselAnimation {
    self.carouselTextViewFrameIndex = (self.carouselTextViewFrameIndex + 1) % NUM_CAROUSEL_TEXT_FIELD_FRAMES;

    [UIView animateWithDuration:2.0
                          delay:2.0
                        options:UIViewAnimationOptionTransitionNone
                     animations:^{
                         carouselTextView.frame = _carouselTextViewFrames[self.carouselTextViewFrameIndex];
                     }
                     completion:^(BOOL finished){
                         [self nextCarouselAnimation];
                     }];
}
completion:^(BOOL finished){
    [self methodYouNamed];
}