删除自定义UITableViewCell时的子视图剪切

时间:2014-02-09 19:18:10

标签: objective-c uitableview uiview uiviewanimation

我今天一直在努力解决这个问题,无法找到合适的解决办法。我可以在单元格上使用自定义动画真的很乱,但那会很脏。我无法弄清楚为什么这个问题首先发生。

Here's a video问题。 (请注意,我已经放慢了动画的速度,因此它非常清晰)

我正在根据MCSwipeTableViewCell的设置制作自定义UITableViewCell。这是滑动/删除时的基本设置:

UITableViewCell
    Cell Screenshot
    Color View
        Button Container
            Button
            Button
            Button
    ContentView

屏幕截图是滑动时动画的视图。按钮容器是从屏幕上滑动按钮的东西。最后,颜色视图是改变背景颜色的视图。

我尝试过所有不同的UITableViewRowAnimation,其中Fade是最好看的,但它并不是我想要的。

我也尝试将色彩视图添加到内容视图和背景视图中,没有任何变化。

以下是目前视图创建的内容:

_buttonColorView = [[UIView alloc] initWithFrame:self.bounds];
_buttonColorView.backgroundColor = self.defaultColor ?: [UIColor clearColor];
[self addSubview:_buttonColorView];

_slidingView = [[UIView alloc] initWithFrame:self.bounds];
[_buttonColorView addSubview:_slidingView];

_contentScreenshotView = [[UIImageView alloc] initWithImage:contentViewScreenshotImage];
[self addSubview:_contentScreenshotView];

这是超级简单的删除代码,它在单元格滑动动画的委托回调中触发:

NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

[self.items removeObjectAtIndex:indexPath.row];

[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
[self.tableView endUpdates];

编辑:我正在谈论的黑客就是这个。与淡入淡出动画一起使用它虽然不是很完美,但却很不错。

我将此方法添加到自定义单元格中:

- (void)commitDeleteAnimations:(UITableViewRowAnimation)rowAnimation {
    CGRect frame = self.buttonColorView.frame;

    if (rowAnimation == UITableViewRowAnimationTop) {
        frame.origin.y = frame.size.height;
        frame.size.height = 0;
    }
    else {
        frame.size.height = 0;
    }

    [UIView animateWithDuration:0.25 animations:^{
        self.buttonColorView.frame = frame;
        self.contentView.frame = frame;
    }];
}

然后在删除时,执行以下操作:

NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

[self.items removeObjectAtIndex:indexPath.row];

[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];

[cell commitDeleteAnimations:UITableViewRowAnimationFade];

再次,非常hacky。

0 个答案:

没有答案