3

I have a UITableViewCell subclass that does its drawing in a drawRect: method. The whole rectangle is custom drawn, including the background. I was able to get very complex cells while keeping the scrolling very smooth.

My problem: I call [table deselectRowAtIndexPath:path animated:YES]; whenever the view appears, in order to comply with Apple's HIG. However, no animation occurs. It worked when I had a custom view (created with subviews) that was transparent (so Apple's background would appear below), of course. Now it doesn't.

My drawRect: is called once during the "animation time", about halfway through. I think this happens because it's animating the highlighted property of the cell from 1 to 0 and it snaps to 0 when it drops below 0.5.

How can I animate this transition? My guess would be to use the usual beginAnimations: etc. and animate a custom floating point field in my cell object from 1 to 0. Will this call drawRect: repeatedly to animate?

Update I managed to get this almost working. I've overridden setSelected:animated: like so:

- (void) setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:NO];

    if (animated) {
        [CATransaction begin];
        CATransition* animation = [CATransition animation];
        animation.type = kCATransitionFade;
        animation.duration = 0.6;
        [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
        [view.layer addAnimation:animation forKey:@"deselectRow"];
        [CATransaction commit];
    }
}

This works perfectly if the table view is on-screen. But if I'm returning to the table view from navigation (back), instead of fading from selected to not selected, it fades from invisible to not selected. What can cause this?

Sergey Kuryanov
  • 6,114
  • 30
  • 52
mips
  • 278
  • 1
  • 5
  • 9
  • So how does your implementation of `setSelected:animated:` in your `UITableViewCell` subclass look? – Ole Begemann Oct 20 '10 at 23:06
  • My question is basically how to code that :) – mips Oct 20 '10 at 23:07
  • You would need to override the method, to avoid call the code defined on the super class, and create your own animation. If you don't know on how to animate Google how to do it. – vfn Oct 21 '10 at 03:10
  • I know how to animate, I was just wondering if animating a custom floating point field with beginAnimation worked or not before I actually start coding, can you confirm that? – mips Oct 21 '10 at 07:37
  • Okey, animating a custom field doesn't work. Even if I override the setter for the animated property and make it call setNeedsDisplay. Now I'm lost. – mips Oct 21 '10 at 19:36

1 Answers1

7

A little late answer, but might still be useful to you and others. What you need to do, is to assign your custom selected view before messaging the super, like so:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    UIView *view = [[UIView alloc] initWithFrame:self.frame];
    view.backgroundColor = [UIColor colorWithRed:.9 green:.0 blue:.125 alpha:1.0];

    self.selectedBackgroundView = view;

    [super setSelected:selected animated:animated];
}
Ronni Egeriis Persson
  • 2,209
  • 1
  • 21
  • 43
  • I used a UIImageView and a custom selectiongradient. It's kinda fake, but looks perfect to me. Thanks! – steipete Feb 27 '11 at 11:45
  • 6
    You should set the cell's selectedBackgroundView when it is created, not in the setSelected:animated: method; this will result in it being set over and over again needlessly. – titaniumdecoy Jan 14 '12 at 21:30