滑动UITableViewCell

我的目标是让UITableViewCell从屏幕的一侧滑落(如在Twitter中),然后从另一侧滑回。 我能够使单元格滑出屏幕右侧,但我似乎无法弄清楚如何让它从左后滑回到屏幕上。 这里是我的代码将其向右滑动:

 UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath]; [cell.layer setAnchorPoint:CGPointMake(0, 0.5)]; [cell.layer setPosition:CGPointMake(cell.frame.size.width, cell.layer.position.y)]; CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.x"]; [animation setRemovedOnCompletion:NO]; [animation setDelegate:self]; [animation setDuration:0.14]; [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]]; [cell.layer addAnimation:animation forKey:@"reveal"]; 

谢谢你的帮助!

UITableView提供了插入和删除animation的方法,并为您处理所有的animation。 尝试这样的事情:

 [tableView beginUpdates]; [tableView deleteRowsAtIndexPaths:myIndexPaths withRowAnimation:UITableViewCellRowAnimationRight]; [tableView endUpdates]; 

然后在一个新的单元格中滑动:

 [tableView beginUpdates]; [tableView insertRowsAtIndexPaths:myNewIndexPaths withRowAnimation:UITableViewCellRowAnimationLeft]; [tableView endUpdates]; 

beginUpdates/endUpdates方法会使animation成批并立即执行。

谨防2件事:

  1. 有了大量的表格数据,插入可能需要很长时间(特别是如果你基本上replace整个表格)。 调用[tableView reloadData]在这种情况下效果更好,但是deleteRowsAtIndexPath:withRowAnimation:仍然可以用于很好的视觉效果。

  2. 您必须确保支持您的表的实际数据相应且正确地更改。 也就是说,如果您要从表中删除或插入行,那么您的数组(或其他)正在馈送表的数据也必须正确反映每个步骤中表中有多less行。

你想要的行为应该通过animation细胞的子视图来完成,而不是细胞本身。 牢房应该留在桌子上。 在contentArea中使用你想要的子视图创build一个自定义的单元格,然后按照你的喜好滑动它们。

标准的基于UIView块的animation应该也适用于此。