视图未按预期显示

时间:2013-06-19 18:14:01

标签: ios objective-c view uitableview jquery-animate

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

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

但是,只有细胞离开屏幕。它没有灰色视图。

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

示例视频here

2 个答案:

答案 0 :(得分:1)

最大的错误是您没有将sideView初始化为任何内容。

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

答案 1 :(得分: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);
    }];

希望这有帮助!

相关问题