[英]Custom animation for changing height of UITableViewCell
I want to make animation of changing UITableViewCell height with UIView animation (in future with spring animation). 我想用UIView动画制作改变UITableViewCell高度的动画(将来用弹簧动画制作)。
So I have: 所以我有:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor greenColor];
}
UILabel *viewLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 10.0f, [[UIScreen mainScreen] bounds].size.width, 40.0f)];
viewLabel.text = @"Test";
viewLabel.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:viewLabel];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView beginUpdates];
[self animateCell:indexPath andTableView:tableView];
[tableView endUpdates];
}
- (void)animateCell:(NSIndexPath*)indexPath andTableView:(UITableView*)tableView
{
[UIView animateWithDuration:1.0f animations: ^
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
CGRect rect = cell.frame;
rect.size.height = 90.0f;
cell.frame = rect;
NSLog(@"%f", cell.frame.size.height);
}];
}
But it does not work. 但它不起作用。 But if I`m adding:
但如果我补充说:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath isEqual:[tableView indexPathForSelectedRow]])
{
return 90.0f;
}
return 40.0f;
}
I see that cell is changing its height and animation is applied to custom cell that is created in animateCell method 我看到该单元格正在改变其高度,动画将应用于在animateCell方法中创建的自定义单元格
I think you should not use beginUpdate
/ endUpdates
methods and perform animations manually before calling reloadData
. 我认为你不应该使用
beginUpdate
/ endUpdates
方法并在调用reloadData
之前手动执行动画。 The final state of cells (after animation) are defined with datasource. 单元格的最终状态(动画之后)是使用数据源定义的。 Here's an example of animation in
tableView:didSelectRowAtIndexPath:
这是
tableView:didSelectRowAtIndexPath:
的动画示例tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_selectedIndexPath = indexPath;
[UIView animateWithDuration:1 animations:^{
// move down cells which are below selected one
for (UITableViewCell *cell in tableView.visibleCells) {
NSIndexPath *cellIndexPath = [tableView indexPathForCell:cell];
if (cellIndexPath.row > indexPath.row) {
CGRect frame = cell.frame;
frame.origin.y += kTargetHeight - tableView.rowHeight;
cell.frame = frame;
}
}
// expand selected cell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
CGRect frame = cell.frame;
frame.size.height = kTargetHeight;
cell.frame = frame;
} completion:^(BOOL finished) {
[tableView reloadData]; // commit final state
}];
}
The final state of selected cell: 所选单元格的最终状态:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath isEqual:_selectedIndexPath]) {
return kTargetHeight;
}
return tableView.rowHeight;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.