I designed following solution:
Subclass UITableViewCell. Define colors of background for selected and not selected cell, e.g.:
#define SELECTED_BACKGROUND_COLOR [UIColor redColor]
#define NOT_SELECTED_BACKGROUND_COLOR [UIColor whiteColor]
(You can create specially designed properties also)
Then, you have to override two methods of the UITableViewCell.
I introduce also simple animation, since as @lemnar stated, backgroundColor property is not animatable. The animation could be much better, with the use of NSTimers, but this will make the code even longer.
- (void) mixBackgroundColorWithSelectedColorMultiplier:(NSNumber *)multiplier
{
CGFloat *selComponents = (CGFloat *) CGColorGetComponents(SELECTED_BACKGROUND_COLOR.CGColor);
CGFloat *notSelComponents = (CGFloat *) CGColorGetComponents(NOT_SELECTED_BACKGROUND_COLOR.CGColor);
if((CGColorGetNumberOfComponents(SELECTED_BACKGROUND_COLOR.CGColor) == 2)
{
selComponents[2] = selComponents[1] = selComponents[0];
}
if((CGColorGetNumberOfComponents(NOT_SELECTED_BACKGROUND_COLOR.CGColor) == 2)
{
notSelComponents[2] = notSelComponents[1] = notSelComponents[0];
}
CGFloat m = [multiplier floatValue];
self.backgroundColor = [UIColor colorWithRed:(notSelComponents[0]) * (1.0 - m) + (selComponents[0]) * m
green:(notSelComponents[1]) * (1.0 - m) + (selComponents[1]) * m
blue:(notSelComponents[2]) * (1.0 - m) + (selComponents[2]) * m
alpha:1.0];
[self setNeedsDisplay];
}
- (void) setSelected:(BOOL)selected animated:(BOOL)animated
{
if(selected)
{
if(animated)
{
[self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.2] afterDelay:0.08];
[self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.4] afterDelay:0.16];
[self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.6] afterDelay:0.24];
[self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.8] afterDelay:0.32];
[self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:1.0] afterDelay:0.40];
}
else
{
[self mixBackgroundColorWithSelectedColorMultiplier:[NSNumber numberWithFloat:1.0]];
}
}
else
{
if(animated)
{
[self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.8] afterDelay:0.08];
[self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.6] afterDelay:0.16];
[self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.4] afterDelay:0.24];
[self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.2] afterDelay:0.32];
[self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.0] afterDelay:0.40];
}
else
{
[self mixBackgroundColorWithSelectedColorMultiplier:[NSNumber numberWithFloat:0.0]];
}
}
[super setSelected:selected animated:animated];
}
- (void) setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
if(highlighted)
{
if(animated)
{
[self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.2] afterDelay:0.08];
[self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.4] afterDelay:0.16];
[self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.6] afterDelay:0.24];
[self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.8] afterDelay:0.32];
[self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:1.0] afterDelay:0.40];
}
else
{
[self mixBackgroundColorWithSelectedColorMultiplier:[NSNumber numberWithFloat:1.0]];
}
}
else
{
if(animated)
{
[self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.8] afterDelay:0.08];
[self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.6] afterDelay:0.16];
[self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.4] afterDelay:0.24];
[self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.2] afterDelay:0.32];
[self performSelector:@selector(mixBackgroundColorWithSelectedColorMultiplier:) withObject:[NSNumber numberWithFloat:0.0] afterDelay:0.40];
}
else
{
[self mixBackgroundColorWithSelectedColorMultiplier:[NSNumber numberWithFloat:0.0]];
}
}
[super setHighlighted:highlighted animated:animated];
}