Animation Block - Completion fires immediately - ios

Why does the following code log 'Done' as soon as it is fired?
[UIView animateWithDuration:0.3
animations:^{
NSLog(#"Start");
}
completion:^(BOOL finished){
NSLog(#"done");
}
];

Because you're not actually animating anything (a NSLog cannot be animated). You need to animate an object or else the completion block will be called straight away. The 0.3 second duration will be ignored if there is nothing being animated.

Because you aren't animating anything. If you change the value of some UIView in your animation block, you will correctly see "done" outputted after the 0.3 second delay.
I tested with a 5 second delay using exactly your code, only adding in something to animate to confirm.

Related

Block-Based Animation does not Work: Animations and Completion Blocks are Executed Immediately (Without Duration)

I tried to animate some features of the UIView, anyway, I simplified everything to these code lines in "ViewController.m":
- (void)viewDidLoad {
...
[UIView animateWithDuration: 5.0 animations:^{
NSLog(#"animations");}
completion:^(BOOL finished){NSLog(#"completion");}];
}
Both blocks outputs immediately after loading despite the fact that animation should last five seconds.
You can't use block animations in viewDidLoad because the UIView you want to animate doesn't have superview at the present moment. Move the code to the viewDidAppear method.

ios animation. How nested animations affect duration?

Let's say I have a timed UIAnimation, in the format of
[UIView animateWithDuration:DURATION_X
animations:^
{
[function that might contain a separate animation with DURATION_Y]
}
completion:^(BOOL finished)
{
[function that might contain a separate animation with DURATION_Z]
}];
If the nested animations also have durations inside them, how does that affect the outer animation? Are the inner animations executed with DURATION_Y and DURATION_Z, respectively, no matter what DURATION_X happens to be? Or is DURATION_Y animations scaled down or curtailed to end with respect to DURATION_X, assuming DURATION_X <= DURATION_Y?
Basically, what I'm asking in a nutshell is how we can control duration of inner animations safely, to ensure they execute with DURATION_Y (and not some shorter version) even when the outermost animation has a different and shorter DURATION_X?
AFAIU, the inner animation takes control over the duration which is mentioned in the outer animation block. Whereas, function inside the completion block does not seem to wait until the main animation block finishes its animation task with the given time duration.
So, lets say DURATION_X is 5 seconds, DURATION_Y is 20 seconds and DURATION_Z is 10 seconds respectively. What happens is that the animation function inside the completion block gets triggered once the control completes (going to the function and do whatever) the main animation block (Having said the function inside the main animation block does have a different time duration). Hence, the function inside the completion block gets executed with the DURATION_Z and it does animate for the exact duration mentioned in DURATION_Z.
Along the side, the function inside the main animation block gets executed for the time duration specified in the DURATION_X ONLY. It ignores the time duration of DURATION_Y. A wacky code snippet here is:
-(void)doSomeAnim
{
[UIView animateWithDuration:5.0
animations:^
{
[self animateOrangeBoy];
}
completion:^(BOOL finished)
{
[self animateBlueBoy];
}];
}
-(void)animateOrangeBoy
{
[UIView animateWithDuration:20.0 animations:^{
orangeView.frame = CGRectMake(orangeView.frame.origin.x, orangeView.frame.origin.y + 300, orangeView.frame.size.width, orangeView.frame.size.height);
}];
}
-(void)animateBlueBoy
{
[UIView animateWithDuration:10.0 animations:^{
blueView.frame = CGRectMake(blueView.frame.origin.x, blueView.frame.origin.y + 300, blueView.frame.size.width, blueView.frame.size.height);
}];
}
So, animateBlueBoy also starts along with the animateOrangeBoy but animateOrangeBoy lasts only 5 seconds but animateBlueBoy goes till 10 seconds.

2 animations on same object, only last shows. How to show first one w/o nesting completion block?

I'm doing two animations on the same UIImageView, using blocks. Animations are not quite back to back, but almost; there is some logic inbetween.
animate UIImageView from one location on the view to another.
execute logic that determines whether image is allowed to stay there
if not allowed, undo the animation (UIImageView springs back to original location)
If I implement this as above, only the second animation shows (this is normal behavior from what I understand). If I nest the logic and the second animation block inside the completion block of the first, I see both animations, but there's a fair amount of code to jam into that completion block and it just seems ugly and out of place.
In the non-nested configuration, why does iOS want to cut short the previous animations and execute only the final one, and how can I force it to wait on the first one before going to the next? I don't think it needs to block the main thread or "sit and spin" in a completion block; I just want all animations to be shown. Tried adding delay to second animation to no avail.
Is this a job for CAKeyframeAnimation?
// first animation
[UIView animateWithDuration:0.5
delay:0.0
options: UIViewAnimationCurveLinear
animations:^{movingColor.center = newCenter;
movingColor.transform = scaleTransform;}
completion:^(BOOL finished){ NSLog(#"forward animation done");}];
if (/* ...color is allowed at that location */) {
// do some stuff
} else {
// undo the animation
[UIView animateWithDuration:0.5
delay:0.0
options: UIViewAnimationCurveLinear
animations:^{movingColor.center = origCenter;
movingColor.transform = scaleTransform;}
completion:^(BOOL finished){ NSLog(#"back animation done");}];
}
The second animation should be done conditionally inside the first's completion block. Putting it into a function will make it more readable.
- (void)moveColor:(UIView *)view to:(CGPoint)center delay:(NSTimeInterval)delay completion:(void (^)(BOOL finished))complete {
[UIView animateWithDuration:0.5 delay:delay options:UIViewAnimationCurveLinear animations:^{
view.center = center;
view.transform = scaleTransform; // probably don't need this
} completion:completion];
}
This way your logic can be separate from the animation code
- (void)someMethod {
[self moveColor:movingColor to:newCenter delay:0.0 completion:^(BOOL finished) {
if (/*it can stay*/) {
// stuff
} else {
[self moveColor:movingColor to:origCenter delay:2.0 completion:^(BOOL finished) {}];
}
}];
}
The correct way to do it is as you said to set the second one in the completition of the first one that is the correct way,
You can also adda delay on the start of the other, this may or may not work it depends on alot of variables
Your second animation will have a delay of .5 (time for first animation to complete )
[UIView animateWithDuration:0.5
delay:0.5
options: UIViewAnimationCurveLinear
animations:^{movingColor.center = origCenter;
movingColor.transform = scaleTransform;}
completion:^(BOOL finished){ NSLog(#"back animation done");}];
The animateWithDuration is executed asynchronously, and returns right away. That is, the next line of code is executed without waiting for the animation to finish. You either put your code in the completion block if you want it to be executed after the animation is finished, or you accept that the second animation is started (thus canceling the first) immediately.
If you want to indicate to the user that something was wrong by starting the animation, but not completing it, you could execute the second animation with a delay. Remember to also set the the UIViewAnimationOptionBeginFromCurrentState option if you delay the second animation with less than the duration of the first:
[UIView animateWithDuration:0.5
delay:0.25 //or some number
options: (UIViewAnimationCurveLinear | UIViewAnimationOptionBeginFromCurrentState)
animations:^{movingColor.center = origCenter;
movingColor.transform = scaleTransform;}
completion:^(BOOL finished){ NSLog(#"back animation done");}];

How can I control UIView animation repeat cycle?

I'm afraid that the title is correct english expression but,
I have an animation on iOS with 4 scenes.
I used setAnimationDidStopSelector: method.
Here's the question: How can I make stopping function for the animation?
In iOS 4 and later you should use block based animation. Using these newer methods you can very easily specify some code to run once the animation has finished. For example:
[UIView animateWithDuration:1.0
animations:^{
// This code will be animated for 1 second.
[anObject setAlpha:0.0];
}
completion:^(BOOL finished) {
// This code will be executed once the animation has completed.
[anObject removeFromSuperview];
}];
A little ugly, but pretty lazy:
add a flag to know when it should animate, and when not. Put your animation under if block (or something same) and just switch flag when you need.

UIView's animateWithDuration delay not delaying animation

I am trying to perform an animation on a label where a flip animation happens and after it is done and after a delay, The text of the label changes.
It seems that the delay never happens. The text immediately changes after the flip completes although I am using UIView animateWithDuration:0.5 delay:4.0 in the completion block. If Instead I do a performSelector with Delay in the completion block (the commented statement) it works as expected. Any idea why the delay value is being ignored?
- (void) flipShapeWithText:(NSString *)text {
[UIView transitionWithView:someLabel duration:0.15 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
someLabel.text = text;
}completion:^ (BOOL finished){
// [self performSelector:#selector(updateLabelText:) withObject: #"New Text" afterDelay:4.0];
[UIView animateWithDuration:0.5
delay:4.0
options: UIViewAnimationOptionTransitionCrossDissolve
animations:^{
currentShapeNameLabel.text = #"New Text" ;}
completion:nil];
}];
}
The delay param of animateWithDuration:delay:options:animations:completion specifies the delay before the animation occurs. You are setting the text within the animation block so after the delay is over, the animations begin which immediately changes the text as that change is not animatable. To do what you want, change the text in the completion block as follows:
[UIView animateWithDuration:0.5
delay:4.0
options: UIViewAnimationOptionTransitionCrossDissolve
animations:^{ // anything animatable }
completion:^(BOOL finished) {
currentShapeNameLabel.text = #"New Text" ;}];
You can eliminate the delay if you want the animation to start immediately. If you want the text change to happen 4 secs after the animation completes add that delay in the completion block either with dispatch_after() or performSelector:withDelay:.
In my case, the problem was that earlier in the code I was calling UIView's snapshotViewAfterScreenUpdates with a value true. After changing that to false it worked fine.
try nesting in
dispatch_async(dispatch_get_main_queue(), ^{
});

Resources