问题
I want to allow the user to switch between a few color palettes in the application, IE how XCode and other text editors allow you to switch between light and dark backgrounds. I'm able to do all this simply enough, but now I'm trying to wrap the change in a simple animation block so the color changes fade in. Everything works great with the exception of my table view cells. The colors change, but they don't animate.
[UIView animateWithDuration:0.5 animations:^{
self.tableView.backgroundColor = [UIColor whiteColor];
for (UITableViewCell *cell in self.tableView.visibleCells)
{
cell.textLabel.textColor = [UIColor blueColor];
}
}];
I'm attempting not to reload the entire table, as that will cause a lot of things to layout again which I don't want. Frankly, I've tried it a couple times and it doesn't work anyway.
For what it's worth, my UITableView is grouped, though I don't think that's really effecting my solution.
The answers listed here are interesting, but I don't believe are relevant to my problem - I am changing text color which never animates: Animating UITableViewCell's backgroundColor in block animation
回答1:
The problem is that text properties are not animatable. In order to animate your changes I would recommend adding a custom view in your table cell's contentView
than you can use a view transition animation to swap the old view for the new one (with the new colours set). This will mean doing your own layout for the view you put into the contentView
since you're not using one of the predefined styles any more.
See docs here: http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html
回答2:
Thanks guys, @shadowhorst link was really helpful. I was able to accomplish what I wanted by using a cross-dissolve transition instead of the animateWithDuration method. The code is below -
[UIView transitionWithView:self.tableView
duration:0.25
options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAllowAnimatedContent
animations:^{
self.tableView.backgroundColor = [UIColor whiteColor];;
for (UITableViewCell *cell in self.tableView.visibleCells)
{
cell.textLabel.textColor = [UIColor blueColor];
}
}
completion:nil];
I did notice that there are still some artifacts around the edges of my grouped cells, which is why I dropped the duration of my animation down to a quarter of a second.
来源:https://stackoverflow.com/questions/14689190/how-can-i-animate-a-uitableviewcells-colors