Отменить UIView animateWithDuration до завершения

У меня есть этот код в моем проекте:

- (void) fadeImageView {
    [UIView animateWithDuration:1.0f
                          delay:0
                        options:UIViewAnimationCurveEaseInOut
                     animations:^{
                         self.imageView.alpha = 0.0f;
                     }
                     completion:^(BOOL finished) {
                         //make the image view un-tappable.
                         //if the fade was canceled, set the alpha to 1.0
                     }];

}

однако есть обстоятельства, когда я хотел бы отменить эту операцию до того, как изображение станет невидимым. Есть ли способ отменить анимацию анимации?

Ответы

Ответ 1

это может помочь

[UIView animateWithDuration:0.001 animations:^{
    [UIView setAnimationBeginsFromCurrentState:YES];
    self.imageView.alpha = 1;
}];

Ответ 2

Прежде всего, вы должны добавить UIViewAnimationOptionAllowUserInteraction к опции, например..

- (void) fadeImageView {
    [UIView animateWithDuration:1.0f
                          delay:0
                        options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                         self.imageView.alpha = 0.0f;
                     }
                     completion:^(BOOL finished) {
                         //make the image view un-tappable.
                         //if the fade was canceled, set the alpha to 1.0
                     }];

}

а затем сделайте такой метод....

-(void)stopAnimation {
    [self.routeView.layer removeAllAnimations];
}

после этого, когда вы хотите удалить свой анимационный вызов выше метода, используя.....

[self performSelectorOnMainThread:@selector(stopAnimation) withObject:nil waitUntilDone:YES];

Надеюсь, это поможет вам.

Счастливое кодирование.........!!!!!!!!!!!!:)

EDIT:

Спасибо user1244109, чтобы помочь мне в этом.

Для iOS7 мы должны добавить еще один параметр UIViewAnimationOptionBeginFromCurrentState, например:

[UIView animateWithDuration:1.0f
                              delay:0
                            options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState
                         animations:^{
                             self.imageView.alpha = 0.0f;
                         }
                         completion:^(BOOL finished) {
                             //make the image view un-tappable.
                             //if the fade was canceled, set the alpha to 1.0
                         }];

Ответ 3

Из документов Apple: Использование этого метода не рекомендуется в iOS 4.0 и более поздних версиях. Вместо этого вы должны использовать метод animateWithDuration:delay:options:animations:completion:, чтобы указать анимацию и параметры анимации.:

[UIView animateWithDuration:1.f
                      delay:0
                    options:UIViewAnimationOptionBeginFromCurrentState
                 animations:^{
                     self.imageView.alpha = 0.0f;
} completion:NULL];