完成前取消一个UIView animateWithDuration(cancel a UIView

2019-07-31 03:48发布

我在我的项目的代码:

- (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
                     }];

}

但是,也有情况在这里我想取消此操作前的ImageView已变得不可见。 有没有办法取消这个动画中旬动画?

Answer 1:

更新:喜欢这个答案https://stackoverflow.com/a/21527129/194309从博鲁特Tomazin



Answer 2:

从苹果的文档: 此方法的使用中的iOS 4.0及更高版本气馁。 相反,你应该使用 animateWithDuration:delay:options:animations:completion: 方法来指定动画和动画选项:

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


Answer 3:

首先你必须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];

希望它会帮助你

快乐编码......... !!!!!!!!!!!! :)

编辑:

由于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
                         }];


文章来源: cancel a UIView animateWithDuration before completion