重新加载数据时 TableViewCell 中的动画
Animation in TableViewCell at reloading data time
我有 UITableViewCell
带有指向顶部或向下的详细箭头。
然后我点击这个单元格,我想展开它。所以我会重新加载 UITableView and I want to animate rotation of
UIImageView`.
问题 - 是否可以在 UITableView
中重新加载数据的同时动画旋转 UIImageView
?
是的,您可以使用 expandable/contactable 个单元格创建一个 table,并且在展开时您可以为单元格的容器设置动画。
但是,一件事直到并且除非它是重新加载的要求 table,我建议您重新加载同时执行动画的行。
此外,正如您在评论中询问的那样,跟踪单元格是需要简单显示还是详细显示的最佳方式是什么,我认为这取决于您的要求,比如您一次只需要显示一个展开的单元格,那么你只需将当前展开的单元格的索引保留在 class 中,否则如果你需要一次显示多个展开的单元格,那么你可以使用数据源来保留单元格是否需要显示为的信息简单或详细。如果您没有数据源,那么您可以通过创建一个单元格索引路径列表来跟踪详细单元格。
您需要以编程方式执行以下操作以显示简单/详细的单元格-
注意:我假设一次只显示一个展开的单元格。
1) 创建一个成员变量来跟踪展开的单元格。
NSIndexPath *expandedCellIndexPath;
2) Return 扩展单元格的高度(如果展开)否则为简单单元格
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([expandedCellIndexPath compare:indexPath] == NSOrderedSame)
return 100.0f;
return 50.0f;
}
3) 创建单元格为-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cellId";
CustomCell *customCell = nil;
customCell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(customCell == nil)
{
NSString *nibName = @"CustomCell_iPhone";
customCell = (CustomCell *)[[[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil] lastObject];
customCell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if([expandedCellIndexPath compare:indexPath] == NSOrderedSame)
{
[customCell.m_arrowImageView setImage:[UIImage imageNamed:DOWN_ARROW_KEY]];
[self showSelectedCellAnimations:customCell];
}
else
{
[customCell.m_arrowImageView setImage:[UIImage imageNamed:UP_ARROW_KEY]];
[self showUnSelectedCellAnimations:customCell];
}
// you can also set default up arrow image and rotate it with down arrow image and vice versa
return customCell;
}
4) 使用-
旋转Up/Down 箭头图像
- (void)rotateImage:(UIImageView *)image duration:(NSTimeInterval)duration
curve:(int)curve degrees:(CGFloat)degrees
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
[UIView setAnimationBeginsFromCurrentState:YES];
CGAffineTransform transform =
CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degrees));
image.transform = transform;
[UIView commitAnimations];
}
5) 将上述方法调用为-
[self rotateImage:customCell.m_helpImage duration:0.25
curve:UIViewAnimationCurveEaseIn degrees:0.0];
6) 在 didSelectRowAtIndexPath:
内将 expandedCellIndexPath 设置为需要扩展的当前索引路径,并重新加载先前扩展的行以收缩并重新加载当前的 expandedCellIndexPath 行以扩展,如-
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
CustomCell *customCell = nil;
customCell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
NSIndexPath *previousIndexPath = expandedCellIndexPath;
if([expandedCellIndexPath compare:indexPath] != NSOrderedSame)
expandedCellIndexPath = indexPath;
[self.m_tableView reloadRowsAtIndexPaths:@[previousIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
[self.m_tableView reloadRowsAtIndexPaths:@[expandedCellIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
[self.m_tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
我有 UITableViewCell
带有指向顶部或向下的详细箭头。
然后我点击这个单元格,我想展开它。所以我会重新加载 UITableView and I want to animate rotation of
UIImageView`.
问题 - 是否可以在 UITableView
中重新加载数据的同时动画旋转 UIImageView
?
是的,您可以使用 expandable/contactable 个单元格创建一个 table,并且在展开时您可以为单元格的容器设置动画。
但是,一件事直到并且除非它是重新加载的要求 table,我建议您重新加载同时执行动画的行。
此外,正如您在评论中询问的那样,跟踪单元格是需要简单显示还是详细显示的最佳方式是什么,我认为这取决于您的要求,比如您一次只需要显示一个展开的单元格,那么你只需将当前展开的单元格的索引保留在 class 中,否则如果你需要一次显示多个展开的单元格,那么你可以使用数据源来保留单元格是否需要显示为的信息简单或详细。如果您没有数据源,那么您可以通过创建一个单元格索引路径列表来跟踪详细单元格。
您需要以编程方式执行以下操作以显示简单/详细的单元格-
注意:我假设一次只显示一个展开的单元格。
1) 创建一个成员变量来跟踪展开的单元格。
NSIndexPath *expandedCellIndexPath;
2) Return 扩展单元格的高度(如果展开)否则为简单单元格
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([expandedCellIndexPath compare:indexPath] == NSOrderedSame)
return 100.0f;
return 50.0f;
}
3) 创建单元格为-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cellId";
CustomCell *customCell = nil;
customCell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(customCell == nil)
{
NSString *nibName = @"CustomCell_iPhone";
customCell = (CustomCell *)[[[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil] lastObject];
customCell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if([expandedCellIndexPath compare:indexPath] == NSOrderedSame)
{
[customCell.m_arrowImageView setImage:[UIImage imageNamed:DOWN_ARROW_KEY]];
[self showSelectedCellAnimations:customCell];
}
else
{
[customCell.m_arrowImageView setImage:[UIImage imageNamed:UP_ARROW_KEY]];
[self showUnSelectedCellAnimations:customCell];
}
// you can also set default up arrow image and rotate it with down arrow image and vice versa
return customCell;
}
4) 使用-
旋转Up/Down 箭头图像- (void)rotateImage:(UIImageView *)image duration:(NSTimeInterval)duration
curve:(int)curve degrees:(CGFloat)degrees
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
[UIView setAnimationBeginsFromCurrentState:YES];
CGAffineTransform transform =
CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degrees));
image.transform = transform;
[UIView commitAnimations];
}
5) 将上述方法调用为-
[self rotateImage:customCell.m_helpImage duration:0.25
curve:UIViewAnimationCurveEaseIn degrees:0.0];
6) 在 didSelectRowAtIndexPath:
内将 expandedCellIndexPath 设置为需要扩展的当前索引路径,并重新加载先前扩展的行以收缩并重新加载当前的 expandedCellIndexPath 行以扩展,如-
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
CustomCell *customCell = nil;
customCell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
NSIndexPath *previousIndexPath = expandedCellIndexPath;
if([expandedCellIndexPath compare:indexPath] != NSOrderedSame)
expandedCellIndexPath = indexPath;
[self.m_tableView reloadRowsAtIndexPaths:@[previousIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
[self.m_tableView reloadRowsAtIndexPaths:@[expandedCellIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
[self.m_tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}