完成之前取消一个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 }]; } 

不过,在imageview变得不可见的情况下,我想取消这个操作。 有没有办法取消这个animation中animation?

这可能有帮助

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

首先,你必须添加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]; } 

之后,当你想删除你的animation调用上面的方法使用…..

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

从Apple文档: 在iOS 4.0及更高版本中不鼓励使用此方法。 相反,您应该使用 animateWithDuration:delay:options:animations:completion: 方法指定您的animation和animation选项:

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