How to undo an animation based on UIView blocks?

I searched for a lot of SO files and Apple links, but still could not solve my problem.

What I have:

  • with two UIImageViews and 2 UIButtons with them
  • 2 types of animation:
    2.1. the first one is scaled, then down through each image, one after another, only once in viewDidLoad 2.2. second, when a button is pressed (which is a custom button hidden "inside" of each UIImageView), it calls the animation of the corresponding UIImageView - only one, not both (it also scales and then down).
  • As I write for iOS4 +, they tell me to use block-based animations!

What I need:

How to cancel the start of the animation? I managed to undo everything except the last ...: / Here is my piece of code:

[UIImageView animateWithDuration:2.0 delay:0.1 options:UIViewAnimationOptionAllowUserInteraction animations:^{ isAnimating = YES; self.bigLetter.transform = CGAffineTransformScale(self.bigLetter.transform, 2.0, 2.0); } completion:^(BOOL finished){ if(! finished) return; [UIImageView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{ self.bigLetter.transform = CGAffineTransformScale(self.bigLetter.transform, 0.5, 0.5); } completion:^(BOOL finished){ if(! finished) return; [UIImageView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{ self.smallLetter.transform = CGAffineTransformScale(self.smallLetter.transform, 2.0, 2.0); } completion:^(BOOL finished){ if(! finished) return; [UIImageView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{ self.smallLetter.transform = CGAffineTransformScale(self.smallLetter.transform, 0.5, 0.5); } completion:^(BOOL finished){ if (!finished) return; //block letter buttons [self.bigLetterButton setUserInteractionEnabled:YES]; [self.smallLetterButton setUserInteractionEnabled:YES]; //NSLog(@"vieDidLoad animations finished"); }]; }]; }]; }]; 

Somehow smallLetter UIImageView does not work correctly, when you click (via the button) bigLetter cancels the animation correctly ... thanks in advance :)

EDIT: I used this solution, but still have the problem of reducing the size of the smallLetter UIImageView - it does not cancel at all ....

EDIT2: I added this at the beginning of the following / previous methods:

 - (void)stopAnimation:(UIImageView*)source { [UIView animateWithDuration:0.01 delay:0.0 options:(UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction) animations:^ { source.transform = CGAffineTransformIdentity; } completion:NULL ]; } 

the problem remains ...: / no idea how to interrupt the last animation for letters in the animation chain

+69
ios iphone animation ios4 uiimageview
Mar 05 2018-12-12T00:
source share
4 answers

You can stop all animations on the screen by calling:

 [view.layer removeAllAnimations]; 

(You will need to import the QuartzCore environment to call methods on view.layer).

If you want to stop a specific animation, and not all animations, it is best to use CAAnimations explicitly, rather than the UIView helper animation methods, then you will have more detailed control and you can explicitly stop the animation by name.

Apple Core Animation documentation can be found here:

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreAnimation_guide/CreatingBasicAnimations/CreatingBasicAnimations.html

+136
Mar 12 2018-12-12T00:
source share

For iOS 10, use the UIViewPropertyAnimator for animation. It provides methods to start, stop, and pause UIView animation.

  let animator = UIViewPropertyAnimator(duration: 2.0, curve: .easeOut){    self.view.alpha = 0.0 } // Call this to start animation. animator.startAnimation() // Call this to stop animation. animator.stopAnimation(true) 
+28
Dec 30 '16 at 20:20
source share

I will add to Nick the answer that making removeAllAnimations smooth the next idea will be very convenient.

 [view.layer removeAllAnimations]; [UIView transitionWithView:self.redView duration:1.0f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ [view.layer displayIfNeeded]; } completion:nil]; 
+2
Oct 19 '14 at 23:35
source share

When I work with UIStackView animations, in addition to removeAllAnimations() I need to set some values ​​to the initial values, because removeAllAnimations() can set them to an unpredictable state. I have a stackView with stackView and view2 inside, and one view should be visible and the other hidden:

 public func configureStackView(hideView1: Bool, hideView2: Bool) { let oldHideView1 = view1.isHidden let oldHideView2 = view2.isHidden view1.layer.removeAllAnimations() view2.layer.removeAllAnimations() view.layer.removeAllAnimations() stackView.layer.removeAllAnimations() // after stopping animation the values are unpredictable, so set values to old view1.isHidden = oldHideView1 // <- Solution is here view2.isHidden = oldHideView2 // <- Solution is here UIView.animate(withDuration: 0.3, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 1, options: [], animations: { view1.isHidden = hideView1 view2.isHidden = hideView2 stackView.layoutIfNeeded() }, completion: nil) } 
0
Feb 07 '19 at 9:04
source share



All Articles