Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perform a task (in iOS) after animation has finished

So I have this line of code:

[tableView setContentOffset:point animated:YES];

and I want to run another piece of code after the animation ends. My attempt was to throw the animating code (setContentOffset) in a separate method and calling it using:

[self performSelectorOnMainThread:@selector(scrollMethod:) withObject:sender waitUntilDone:YES];

The problem is that the method returns immediately, not after the animation is finished, even though waitUntilDone is YES, but apparently that is how animation works.

I know that I can use thread waiting but it is not clean, so I will only use it as a last resort. (Maybe I would use this if I know the time it takes the scrolling animation to happen.)

Any ideas on how to go about this are welcome.

(P.S. The scenario is this: I am showing a popover, which is displayed perfectly when there is no keyboard, however, if the keyboard is visible, the popover's height shrinks which sometimes reduces it to almost border only. So just before showing the popover, I want to scroll the view upwards so that the popover never pops in the keyboard.)

like image 617
mrd3650 Avatar asked Nov 25 '11 09:11

mrd3650


2 Answers

UITableView inherits setContentOffset:animated: from its superclass, UIScrollView. Check out the scrollViewDidEndScrollingAnimation: method of UIScrollViewDelegate.

like image 135
rob mayoff Avatar answered Oct 27 '22 06:10

rob mayoff


Try to use this method:

[UIView animateWithDuration:0.6f
                      delay:0.0f
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^{
                     // Do your animations here.
                 }
                 completion:^(BOOL finished){
                     if (finished) {
                         // Do your method here after your animation.
                     }
                 }];
like image 25
Kjuly Avatar answered Oct 27 '22 04:10

Kjuly