-1

理论上,下面的代码应该将屏幕的表格视图单元格设置为动画,并在其位置引入一个黑暗的“视图”。

CGPoint location = [gesture locationInView:tableView];
NSIndexPath *swipedIndexPath = [tableView indexPathForRowAtPoint:location];
UITableViewCell *swipedCell  = [tableView cellForRowAtIndexPath:swipedIndexPath];

//code to create view
UIView *sideView;
sideView.backgroundColor = [UIColor lightGrayColor];
//set the side view frame to the same as the cell
sideView.frame = swipedCell.frame;
//add it to the tableview
[tableView addSubview:sideView];

[UIView animateWithDuration:1
                 animations:^{
                     sideView.frame = CGRectMake(0, swipedCell.frame.origin.y, swipedCell.frame.size.width, swipedCell.frame.size.height);
                     // While simultaneously moving the cell's frame offscreen

                     // The net effect is that the side swipe view is pushing the cell offscreen
                     swipedCell.frame = CGRectMake(swipedCell.frame.size.width, swipedCell.frame.origin.y, swipedCell.frame.size.width, swipedCell.frame.size.height); //move cell off
                 }];

但是,只有单元格会移出屏幕。没有灰色视图可以代替它。

我缺少一个步骤吗?这段代码有什么问题?

示例视频在这里

4

2 回答 2

1

最大的错误是你没有初始化sideView任何东西。

尝试UIView* sideview = [[UIView alloc] initWithFrame:swipedCell.frame];

于 2013-06-19T18:22:14.640 回答
0

像这样添加视图代替单元格听起来不是一个好主意。您必须处理滚动、表格视图编辑以及 UITableView 为您处理的其他事情。因此,请尝试将 sideView 添加为子视图,swipedCell.contentView然后改为执行此动画:

[UIView animateWithDuration:1 animations:^{
    sideView.frame = CGRectMake(0, swipedCell.frame.origin.y, swipedCell.frame.size.width, swipedCell.frame.size.height);
    //This moves all the subviews except for the sideView off the screen
    for (UIView *subview in swipedCell.contentView.subviews)
        if (![subview isEqual:sideView])
            subview.frame = CGRectOffset(subview.frame, swipedCell.frame.size.width, 0.0);
    }];

希望这可以帮助!

于 2013-06-19T18:21:43.143 回答