Dim*_*rio 4 uitableview uiview uiviewanimation ios
我想用UIView动画制作改变UITableViewCell高度的动画(将来用弹簧动画制作).
所以我有:
- (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);
}];
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用.但如果我补充说:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath isEqual:[tableView indexPathForSelectedRow]])
{
return 90.0f;
}
return 40.0f;
}
Run Code Online (Sandbox Code Playgroud)
我看到该单元格正在改变其高度,动画将应用于在animateCell方法中创建的自定义单元格
我认为你不应该在调用之前使用beginUpdate/ endUpdates方法并手动执行动画reloadData.单元格的最终状态(动画之后)是使用数据源定义的.这是一个动画的例子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
}];
}
Run Code Online (Sandbox Code Playgroud)
所选单元格的最终状态:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath isEqual:_selectedIndexPath]) {
return kTargetHeight;
}
return tableView.rowHeight;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2993 次 |
| 最近记录: |