Как зациклить UIAnimation?

У меня анимация воспроизводится нормально. Но в конце анимации анимация останавливается. Я хочу, чтобы анимация зацикливалась. Как я могу это сделать? Вот мой код:

    - (void) startTicker
{
    if (self.timerIsRunning) return;

    self.timerIsRunning = YES;

    NSTimer *newTimer = [NSTimer timerWithTimeInterval:(0.1) target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:newTimer forMode:NSDefaultRunLoopMode];

   [UIView beginAnimations:nil context:nil];
   [UIView setAnimationDuration:100.0];
   [UIView setAnimationDelegate:self];
   [UIView setAnimationDelay:0.0];
   [UIView setAnimationDidStopSelector:@selector(moveToLeft:finished:context:)];
   [UIView commitAnimations];


}


     -(void)moveToLeft:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context 
    { 

        imageView.left = 800; 
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:100.0];
        [UIView setAnimationDelay:0.0];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self cache:YES];
        imageView.right = 800;

        [UIView setAnimationDidStopSelector:@selector(moveToLeft2:finished2:context2:)];
        [UIView commitAnimations];



    }

person Yusuf OZKAN    schedule 29.11.2011    source источник
comment
не могли бы вы отредактировать свой вопрос, чтобы добавить в верхние строки кода первой функции, которая, похоже, была отключена?   -  person Michael Dautermann    schedule 29.11.2011
comment
я редактировал чувак. Как я могу решить эту проблему :(   -  person Yusuf OZKAN    schedule 29.11.2011
comment
вы вызываете moveToLeft:finished:context из своего moveToLeft2:finished2:context2 метода? :-)   -  person Michael Dautermann    schedule 29.11.2011
comment
Я думаю, что вы неправильно делаете повторяющиеся / бесконечные анимации. Взгляните на этот повторяющийся вопрос и посмотрите, сможете ли вы найти решение, которое лучше подходит для вас.   -  person Michael Dautermann    schedule 29.11.2011


Ответы (1)


Для циклической анимации лучше всего использовать блочную анимацию, которая является частью UIView.

При вызове метода:

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

Вам, вероятно, понадобится UIViewAnimationOptionAutoreverse в сочетании с UIViewAnimationOptionRepeat.

Например: (цикл мигает красной рамкой)

[UIView animateWithDuration:2.0f delay:0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^{
    [redBorder setAlpha:0]; //first part of animation
    [redBorder setAlpha:0.5]; //second part of animation
} completion:nil];
person POF_Andrew_POF    schedule 01.09.2013