cancel a UIView animateWithDuration before completion
Asked Answered
D

3

20

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?

Devisable answered 24/8, 2012 at 0:12 Comment(4)
possible duplicate of Cancel a UIView animation? Sure it's not block based, but it works.Soar
Note that the proposed edit actually corrects apparently erroneous code. You should be using UIViewAnimationOptionCurveEaseInOut in this case. The value (0) is equivalent to UIViewAnimationCurveEaseInOut (0<<16) but will fail if you change it to other animation curves.Sternwheeler
possible duplicate of How to cancel UIView block-based animation?Debt
I believe that the best way in 2018 would be to use UIViewPropertyAnimatorDimphia
C
8

Update: prefer this answer https://mcmap.net/q/619194/-cancel-a-uiview-animatewithduration-before-completion from Borut Tomazin

Curlicue answered 28/1, 2013 at 22:16 Comment(1)
Documentation states that this method does nothing outside of an animation blockAlignment
D
13

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];
Derayne answered 3/2, 2014 at 12:11 Comment(1)
thanks alot! that worked, whereas solution by @Thunder Rabbit didn't do it on iOS 7.1. Apple was right after all )Batsheva
S
11

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
                         }];
Sastruga answered 4/3, 2013 at 12:54 Comment(1)
this code worked for me on ios 7 only after specifying UIViewAnimationOptionBeginFromCurrentState option too. Edit it if you wish so.Batsheva
C
8

Update: prefer this answer https://mcmap.net/q/619194/-cancel-a-uiview-animatewithduration-before-completion from Borut Tomazin

Curlicue answered 28/1, 2013 at 22:16 Comment(1)
Documentation states that this method does nothing outside of an animation blockAlignment

© 2022 - 2024 — McMap. All rights reserved.