Custom UITableViewCell and animation on setSelected:animated:

会有一股神秘感。 提交于 2019-12-21 05:43:13

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/3982942/custom-uitableviewcell-and-animation-on-setselectedanimated

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