开发者

Custom UITableViewCell and animation on setSelected:animated:

开发者 https://www.devze.com 2023-01-20 21:47 出处:网络
I have a UITableViewCell subclass that does its drawing in a drawRect: method. The whole rectangle is custom drawn, including the background.

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 = [CATra开发者_C百科nsition 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?


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];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消