在完成之前取消UIView animateWithDuration

Sea*_*ser 18 uiview uianimation ios quartz-core

我在我的项目中有这个代码:

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

}
Run Code Online (Sandbox Code Playgroud)

但是,有时我想在imageview变得不可见之前取消此操作.有没有办法取消动画中期动画?

Bor*_*zin 13

来自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];
Run Code Online (Sandbox Code Playgroud)


Nir*_*iya 10

首先,您必须将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
                     }];

}
Run Code Online (Sandbox Code Playgroud)

然后制作一个像这样的方法....

-(void)stopAnimation {
    [self.routeView.layer removeAllAnimations];
}
Run Code Online (Sandbox Code Playgroud)

之后当你想要删除动画调用上面的方法使用.....

[self performSelectorOnMainThread:@selector(stopAnimation) withObject:nil waitUntilDone:YES];
Run Code Online (Sandbox Code Playgroud)

希望它会对你有所帮助

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

编辑:

感谢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
                         }];
Run Code Online (Sandbox Code Playgroud)


Thu*_*bit 8

更新:更喜欢这个答案来自Borut Tomazin的/sf/answers/1506899061/

  • 这在iOS 7中对我不起作用.Borut的确如此. (2认同)
  • 文档声明此方法在动画块之外不执行任何操作 (2认同)