本文介绍了自定义UITableViewCell和setSelected上的动画:动画:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableViewCell 子类,它在drawRect:方法中进行绘制。整个矩形是自定义绘制的,包括背景。
我能够在保持滚动非常平滑的同时获得非常复杂的单元格。

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.

我的问题:我调用 [table deselectRowAtIndexPath:path动画:是]; 每当视图出现时,为了符合Apple的HIG。但是,不会发生动画。当我有一个透明的自定义视图(用子视图创建)时(当时Apple的背景会出现在下面),它可以工作。现在它没有。

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.

我的drawRect:在动画时间中被调用一次,大约一半。我认为这是因为它将单元格的突出显示属性从1设置为0,当它降至0.5以下时会捕捉到0。

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.

如何设置此转换的动画?我的猜测是使用通常的beginAnimations:等,并在我的单元格对象中设置一个自定义浮点字段,从1到0.这会反复调用 drawRect:来制作动画吗?

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?

更新
我设法让这个几乎正常工作。我已经覆盖了setSelected:animated:就像这样:

UpdateI 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?

推荐答案

有点迟到的答案,但对你和其他人可能仍然有用。您需要做的是在发送超级邮件之前分配您自定义选择的视图,如下所示:

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

这篇关于自定义UITableViewCell和setSelected上的动画:动画:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:16