bopa
bopa

Reputation: 1145

completion block of uiview animate never called if view disappear

i noticed some strange behavior. When i start a animation and change the View (the view will not dismissed!), the completion handler never get called.

[UIView animateWithDuration:0.1f 
                          delay:0.0f 
                        options:UIViewAnimationCurveEaseOut 
                     animations:^(void){
                         [myView setHidden:YES];
                         myLabel.alpha = 0.0f;
                         someOtherView.frame = CGRectMake(130, bubbleBigRect.origin.y, 61, 65);
                         [button setHidden:YES];
                     } 
                     completion:^(BOOL finished){
                         NSLog(@"Complete %d",finished);
                         [imageVIew setImage:[UIImage imageNamed:@"myPng.png"]];                    
                     }];
}

is there any solution for this?

Upvotes: 8

Views: 4578

Answers (4)

Muhammad Nayab
Muhammad Nayab

Reputation: 93

In my case the view which I was animating has its frame set to (0,-568,320,568) and after animation I forget to change the frame to its required position i.e. (0,0,320,568) so the completion block was not being called. Changing the frame of the animated view did the job for me. So I can say if for instance the view has nothing to show(as the frame was not set in visible region). The completion block may not called.

Upvotes: 0

Zev Eisenberg
Zev Eisenberg

Reputation: 8168

Move your call to [myView setHidden:YES] from the animations block to the completion block. I think that will probably help. You can still set the alpha of myView to 0 during the animation block (if you want it to fade out), or before the entire call to -[UIView animateWithDuration:delay:options:animations:completion:] if you want it to disappear right away.

Upvotes: -1

Aurelien Porte
Aurelien Porte

Reputation: 2702

Put this in your animation block, and put everything you want to do on completion in YOUR_SELECTOR method. You can now do whatever you want with your view and still have a way of executing something on completion!

[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(YOUR_SELECTOR)];

Upvotes: 1

shem
shem

Reputation: 4712

Don't know where to write it, but I get the same thing that you have, but in my case the completion block was sometimes called. It might be same thing.
I found out that if in the animation block has nothing was animate- for example, if you set alpha=0 to uiview that is alpha is already 0, or you set content offset to UIScrollView to the same content offset (like in my case), the completion block not called.

Upvotes: 5

Related Questions