gpt4 book ai didi

objective-c - 滑动 UITableViewCell

转载 作者:可可西里 更新时间:2023-11-01 17:02:37 27 4
gpt4 key购买 nike

我的目标是让 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 提供了使用动画插入和删除行的方法,并为您处理所有动画。尝试这样的事情:

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

然后滑入一个新单元格:

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

beginUpdates/endUpdates 方法导致动画被批量处理并同时执行。

注意两件事:

  1. 对于大量表数据,插入可能需要很长时间(特别是如果您基本上要替换整个表)。在这种情况下,调用 [tableView reloadData] 效果更好,但 deleteRowsAtIndexPath:withRowAnimation: 仍可用于获得良好的视觉效果。

  2. 您必须确保支持表格的实际数据相应地正确更改。也就是说,如果您要从表中删除或插入行,那么为表提供数据的数组(或其他任何数组)也必须正确反射(reflect)每个步骤中表中的行数。

关于objective-c - 滑动 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5640820/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com