19

我的项目中有这段代码:

- (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 变得不可见之前取消此操作。有没有办法取消这个动画中间动画?

4

3 回答 3

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];
于 2014-02-03T12:11:56.327 回答
11

首先,您必须将 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
                         }];
于 2013-03-04T12:54:30.697 回答
8

更新:更喜欢Borut Tomazin 的这个答案https://stackoverflow.com/a/21527129/194309

于 2013-01-28T22:16:30.457 回答