Approach for doing animations in drawRect:(CGRect)
I am putting together a feed in a UITableView
. I am adding a custom UIView
to my UITableViewCells
an开发者_如何学Pythond is drawing all the content of the cell in drawRect:(CGRect)
.
This works well, when a user taps a cell I expand the cell and drawRect:(CGRect)
is called again by the UITableViewCell on the subview and everything is drawn to reflect this.
This is great for performance, the UITableViewCell only consists of a single subview, with all content drawn directly into the CGContext.
Now that I am polishing the functionality of the feed I would like to add a few animations to make it more clear to the user what is happening.
I would like to do animations before, during and after the cell expands. the drawRect approach is a "one shot" way of doing it and I understand that the trade off is the same as the advantage, that you don't have a bunch of subview references that you can animate etc. How could I go about implementing animations?
Is there any delegate methods called on the subview when the cellHeight is changed that I can hook into. If I do so will I have to make subview "copies" of all the elements I need to animate, then animate them, remove them and redraw them in the context afterwards?
I am a bit afraid of going down a very wrong path and end up loosing performance and readability of the drawRect if I try to make conditional blocks for every thinkable event/animation that can be displayed.
Hope someone can point in the right direction. Thanks in advance.
When resizing your cell, do this to animate the cell resize process:
[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:... withRowAnimation:YES];
[tableView endUpdates];
Now your cell's drawRect:
will be called for the duration of the animation, which you can measure and implement.
A problem might arise if a consistent framerate is not always achieved and you don't finish the animation.
I think a better solution is to use CALayer
s and animate your drawing there.
精彩评论