How can I animate a UITableViewCell's colors?

情到浓时终转凉″ 提交于 2019-12-25 11:33:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!