我想用另一个类似内容的单元格替换表格单元格动画。
e.g。一个单元格中央对齐标签,其他较大单元格,其他内容在左上角有相同的标签(文本,字体和颜色相同)。知道如何实现吗?
我尝试过:
具有reloadSections:
和reloadRowsAtIndexPath:
功能的标准表格单元格动画。但我无法实现平滑效果,即中心对齐标签应移动到左上角动画,同时更换单元格
[self.tableView beginUpdates];
[self.tableView endUpdates];
只给我换身动画。
答案 0 :(得分:1)
在tableView:willDisplayCell:forRowAtIndexPath:
委托方法中,您可以使用cellForRowAtIndexPath:
方法检索当前显示(即旧)单元格。
使用此功能,您可以使用reloadRowsAtIndexPaths:withRowAnimation:
方法执行此操作
你要做的是:
像:
- (void)tableView:(UITableView *)tableView willDisplayCell:(BaseCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
BaseCell *oldCell = (BaseCell *)[self.tableView cellForRowAtIndexPath:indexPath];
if(oldCell != nil && oldCell != cell) {
CGRect oldFrame = oldCell.titleLabel.frame;
CGRect newFrame = cell.titleLabel.frame;
cell.titleLabel.frame = (CGRect){oldFrame.origin, newFrame.size};
[UIView animateWithDuration:0.25 animations:^{
oldCell.titleLabel.frame = (CGRect){newFrame.origin, oldFrame.size};
cell.titleLabel.frame = (CGRect){newFrame.origin, newFrame.size};
} completion:^(BOOL finished) {
// reset oldCell's titleLabel position for reuse
oldCell.titleLabel.frame = oldFrame;
}];
}
}
您需要关注的一些事项:
UITableViewRowAnimationFade
用于reloadRowsAtIndexPaths:withRowAnimation:
。答案 1 :(得分:0)
表格单元格只有一些预定义的可能动画。
您应该通过更改[UIView animateWithDuration:animations:]
内单元格内的框架来手动设置标签动画。就像你动画任何其他视图一样。
在tableView:cellForRowAtIndexPath:
重新使用/重新配置单元格时,您还必须跟踪标签的状态。
答案 2 :(得分:0)
您可以使用
在单元格上执行动画-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
委托方法。例如,您可以使用UIView
动画或CABasicAnimation
这样淡入细胞:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
[cell setAlpha:0.1];
[UIView animateWithDuration:0.5f animations:^{
[cell setAlpha:1];
}];
}