Cancelling UIView Animation - self.view.layer removeAllAnimations not working - ios

I've got a UIView Animation going on that I need to cancel in my iOS app. I've tried this:
[self.view.layer removeAllAnimations];
But it didn't work. The animation continued. Here is my animation code:
[UIView animateWithDuration:1.4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
recognizer.view.transform = CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y);
} completion:^(BOOL finished) {
NSLog(#"completed animation, now do whatever");
}];
Does anybody have any ideas as to why it's not working?

You are adding that animation to recognizer's view, hence you will have to remove it from that same view's layer.
So instead of
[self.view.layer removeAllAnimations];
you may want to
[recognizer.view.layer removeAllAnimations];
And to keep the current status of the transformation, fetch that one from the presentation layer. The presentation layer is the one that actually reflects the changes during the animation.
recognizer.view.layer.transform = recognizer.view.layer.presentationLayer.transform;

Ok - just figured it out. Changed the component beng animated from the gesture recogniser on top of the image view to the image view itself. Now, just before the code to stop the animation, I have:
truckView.frame = [[trackView.layer presentationLayer] frame];
[truckView.layer removeAllAnimations];
So this is the way to do it. Thanks for the help that led me to this answer,
Sam

Related

Stop specific UIViewAnimation block - iOS

I have an iOS application which runs a few different UIViewAnimation blocks to animate a few different objects on screen. This all works, but how can I stop ONE of the animation blocks WITHOUT stopping the rest?
I have tried using the following method:
[button.layer removeAllAnimations];
But it doesn't do anything, the animation just continues.
I then tried to use a simple BOOL value and get the animation to return from the method once the BOOL is set to "NO", but that doesn't work either.
Here is the animation which I am trying to stop:
-(void)undo_animation:(int)num {
// Fade out/in the number button label
// animation which lets the user know
// they can undo this particular action.
[UIView animateWithDuration:0.5 delay:0.2 options:(UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction) animations:^{
// Mostly fade out the number button label.
((UIButton *)_buttons[num]).alpha = 0.2;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.5 delay:0.2 options:(UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction) animations:^{
// Fade in the number button label.
((UIButton *)_buttons[num]).alpha = 1.0;
} completion:^(BOOL finished) {
// Stop the animation if the BOOL
// is set to 'NO' animations.
if (anim_state_button == NO) {
return;
}
}];
}];
}
Thanks, Dan.
You can't reach running animation property if you're using UIKit Animations. So I suggest to using Core Animation if you want to modify animation in the runtime of it.
And it's too simple to removing alpha of the view like below.
CABasicAnimation* fadein= [CABasicAnimation animationWithKeyPath:#"opacity"];
[fadein setToValue:[NSNumber numberWithFloat:1.0]];
[fadein setDuration:0.5];
[[moviepic layer]addAnimation:fadein forKey:#"MyAnimation"];
after adding animation to layer animation will start and than you can use delegate methods to be informed about animationDidFinish: method
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
NSLog(#"Animation interrupted: %#", (!flag)?#"Yes" : #"No");
}
Also you can reach whenever you want from using;
[[moviepic layer] animationForKey:#"MyAnimation"];
And of course you need to add CoreAnimation Framework to your project.
Hope it helps.
I think the simple way is that remove all animations from your View Layer as all the animation are by default added into the View's Layer.
[yourRequiredView.layer removeAllAnimations];
My understanding is that removing all animations from the relevant layer should stop all animations. How about [((UIButton *)_buttons[num]).layer removeAllAnimations]; ?

how to add bounce animation to animateWithDuration?

I have a simple animation that im performing in my scroll view delegate method scrollViewDidEndDragging.
It looks like this:
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
NSLog(#"finger was lifted");
[UIView animateWithDuration:1.0
animations:^{
self.homeLabel.frame = self.view.frame;
}];
}
Using this animation after lifting the finger the my homeLabel is coming from top, and i want to add it a bounce animation to the label, so when it comes from top, instead of landing smoothly it will have a nice bounce...how can i DO THAT? thanksss
You can use the usingSpringWithDamping animation function.
[UIView animateWithDuration:1.0 delay:0 usingSpringWithDamping:0.2 initialSpringVelocity:5.0 options:UIViewAnimationOptionCurveLinear animations:^{
self.homeLabel.frame = self.view.frame;
} completion:^(BOOL finished) {
}];
Adjusting the Spring Damping and Initial Spring Velocity can give you the effect you want.
One good solution is to create a custom layer for your view that overrides the addAnimation:forKey: method to include a custom timing function.
This answer goes into the specifics of how to do that.
Another option is to take a look at key frame animation. This question and answer covers that approach very well.

After using [self.view.layer removeAllAnimations], the next animation not working

The problem is when I'm using:
[transLabel.layer removeAllAnimations];
(transLabel is a UILabel that show the information)
After that, I try to add a new animation to this Label
[UIView animateWithDuration:showTime
animations:^{
transLabel.hidden = NO;
transLabel.transform = CGAffineTransformMakeScale(1.5, 1.5);
}
completion:^(BOOL finished) {
transLabel.hidden = YES;
transLabel.transform = CGAffineTransformMakeScale(1.0, 1.0);
}];
But It didn't work anymore, it just work at odd number click. Like, first time it works, and second time it doesn't work, but third time it shows again, forth not, fifth yes..
What I want to do is keep pushing down one button again again again
and the Label is show again again again with animation
Thank you and wait for your answer
If i understood your question correctly, the problem occurs because the presentation layer do all the animations and are the layer that actually performs all the animations. When you cancel an ongoing animation that is reflected upon the presentation layer and not the view layer. Try capturing the changes to the view's layer and see if that helps you in this case:
transLabel.layer.transform = transLabel.layer.presentationLayer.transform;
I just had a similar issue. The problem is that when you stop animation you hide your transLabel.
Show it right after [transLabel.layer removeAllAnimations]; like this:
[transLabel.layer removeAllAnimations];
transLabel.hidden = NO;

Pausing a UIView's key frame animations

I'm currently creating a loading bar that I would like to pause and reverse if a specific action is taken.
I have the following code below that animates one view in another. How would I go about pausing this animation and applying another animation on the CURRENT frame at that point in time for the animated object. I would like to be able to animate this bar down to the 0 width mark at any given time within the current animation.
-(void)animateProgressBar
{
CGRect endingFrame = self.frame;
[UIView animateKeyframesWithDuration:self.time delay:0.0 options:0 animations:^{
[UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:1.0 animations:^{
self.loadingBar.frame = endingFrame;
}];
} completion:^(BOOL finished) {
NSLog(#"TADA");
}];
}
If you want to start a new animation from where this one is now, just start the new animation using the the UIViewKeyframeAnimationOptionBeginFromCurrentState option (or if just doing animateWithDuration, use UIViewAnimationOptionBeginFromCurrentState).
You can import QuartzCore framework, add
#import <QuartzCore/QuartzCore.h>
And you can remove existing animation by calling:
[self.loadingBar.layer removeAllAnimations];
After that you can start new animation.
If you want to pause it you have to get reference to the current animation:
CALayer *currentLayer = self.loadingBar.layer.presentationLayer;
And you have to save it:
self.loadingBar.layer.transform = currentLayer.transform;
You can do it in another [UIView animateWith..... method.

When customView transition animation, it can't receive gesture

[UIView transitionWithView:imageView
duration:3.0
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
imageView.image = [UIImage imageNamed:[welcomePhoto objectAtIndex:photoCount]];
}
completion:^(BOOL finish){
}];
When the animation transition at the three second,
imageView can't receive custom gesture,
other times is fine.
How can i fix it? Thanks.
The reason : when UIView animation starts from one point to another, the actual view goes to the new position straight away but it is just hidden. And the view you see moving is just an illusion.
My Given reason is based on paul hagerty's stanford lecture about animation.
The solution to this can be found in other given answer.

Resources