Part of an app I’m working on involves a messaging interface which I’m trying to make similar to the iOS Messages app’s interface. When new messages are received or sent, I’d like the table view to scroll down and display the insertion animation.
So far, I’ve essentially done this:
- (void)didReceiveNewMessage:(...) {
[messages addObject:...]; // add the new message to an array
NSIndexPath *newIndexPath = [NSIndexPath ...]; // find out where the new cell goes
[tableView insertRowAtIndexPath:newIndexPath ...]; // animate the insertion of the new message
[tableView scrollToRowAtIndexPath:newIndexPath ...]; // do the scrolling
}
This works if messages are coming in slowly. However, if I send myself a bunch of messages really quickly, I’ve noticed the table view will “become confused.”
For example, if I quickly send the messages 1, 2, 3, 4, 5, 6, the table view will animate the insertion of 1, 2, 3, and 4, and then just stop scrolling. If I actually scroll the table view down manually, I can see 5 and 6 below. Anyone know of a better way to do this?
I did try setContentOffset: to scroll the table view, but this had the same effect.
The underlying issue here is that I was lazy and used
CGAffineTransformMakeTranslationinstead of manually computing the new frames, because I forgot that when thetransformproperty is changed frames become “undefined” according to Apple documentation.It ended up working okay until lots of messages came in at which point the table view’s frame would get messed up and offset somehow which would cause the bottom of the table view to be displayed slightly lower than it should’ve been.
Lesson learned: Don’t mix frame computations and transformations in one animation. Either keep everything in terms of frames, or keep everything in terms of transformations and use
center+bounds.