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.)
Try to use this method:
You can create animations and tell them directly to perform a block after they're done.
Here's an alternative that may play nicer with the UITableView's animations.
And make sure to implement your
scrollMethod:
with this signature:You can use the context to know which
sender
you have. Read the UIView docs for more on UIView animations.For a scrollView, tableView or collectionView if you do something like this:
then you'll get back a:
when the scroll finishes.
You do NOT get this callback if the user moves the view.
UITableView inherits setContentOffset:animated: from its superclass, UIScrollView. Check out the scrollViewDidEndScrollingAnimation: method of UIScrollViewDelegate.