cancel a UIView animateWithDuration before completion

杀马特。学长 韩版系。学妹 提交于 2020-01-01 04:15:06

问题


I have this code in my project:

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

}

however, there are circumstance where i would like to cancel this operation before the imageview has become invisible. Is there a way to cancel this animation mid animation?


回答1:


Update: prefer this answer https://stackoverflow.com/a/21527129/194309 from Borut Tomazin




回答2:


From Apple docs: Use of this method is discouraged in iOS 4.0 and later. Instead, you should use the animateWithDuration:delay:options:animations:completion: method to specify your animations and the animation options.:

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



回答3:


First of all you have to add UIViewAnimationOptionAllowUserInteraction to option like..

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

}

and then make a method like this....

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

after that when you want to remove your animation call above method using .....

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

Hope it will help you

Happy coding.........!!!!!!!!!!!! :)

EDIT:

Thanks user1244109 to guide me for this.

For iOS7 we have to add one more option UIViewAnimationOptionBeginFromCurrentState like:

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


来源:https://stackoverflow.com/questions/12101708/cancel-a-uiview-animatewithduration-before-completion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!