ios - 在不重新加载的情况下为 UITableViewCell subview 中的帧更改设置动画的最佳方法

标签 ios objective-c animation uitableview subview

在不重新加载单元格(通过 reloadData 或通过重新加载特定单元格和部分)的情况下,是否有动画化 UITableViewCell subview 中的帧更改的最佳实践。

示例:我有一个简单的 UIView 作为自定义 UITableViewCell 的 subview ,其中 subview 占据总行宽的一定百分比。我想为这个 UIView 的宽度变化设置动画(而容器 View ,UITableViewCell 内容 View 保持不变),但不重新加载,因为重新加载动画选项不显示框架 subview 以我喜欢的方式平滑地改变大小。

我最初的解决方案是遍历可见单元格,并使用 UIView 动画 block 手动更改每一帧,例如

for (CustomCell *cell in [self.tableView visibleCells]])
{
    CGRect newFrame = CGRectMake(0.0,0.0,10.0,0.0); // different frame width
    [UIView animateWithDuration:0.5 animations:^{
        cell.block.frame = updatedFrame; // where block is the custom subview
     }];
}

虽然这似乎适用于 iOS7,但在 iOS6 上会导致一些奇怪且难以解决的图形问题;遍历所有单元格似乎有些矫枉过正。

关于执行此操作的最佳方法有什么建议吗?

最佳答案

您可以使用 NSNotificationCenter 发布通知以通知每个单元格更改该特定 View 的宽度。

创建单元格后,您可以将其注册到特定通知:

[[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(receiveNotification:) 
    name:@"Notification"
    object:nil];

然后您可以在每个单元格中处理通知并进行所需的更改:

- (void) receiveNotification:(NSNotification *) notification
{
    CGRect newFrame = CGRectMake(0.0,0.0,10.0,0.0); // different frame width
[UIView animateWithDuration:0.5 animations:^{
    self.block.frame = updatedFrame; // where block is the custom subview
 }];
}

当你想改变宽度时你只需要发布事件。

[[NSNotificationCenter defaultCenter] 
        postNotificationName:@"Notification" 
        object:self];

不要忘记重用每个单元格,当单元格被释放时,请确保取消注册该事件。

[[NSNotificationCenter defaultCenter] removeObserver:self]; 

关于ios - 在不重新加载的情况下为 UITableViewCell subview 中的帧更改设置动画的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19016213/

相关文章:

ios - 如何在 XCODE 中动态设置带有自定义类的自定义单元格中的 UIVIEW?

ios - 从 NSData 加载 UIImage 导致不稳定的 UITableView 滚动

iphone - Objective-C 有选择地从 plist 填充表;如果键等于

iOS,在 UIImage 上绘制带笔划的文本

android - android中旋转动画的缓动效果

ios - 从另一个 anchor 在背景中缩放动画

ios - iCloud 和 Core Data 预填充数据库

ios - 如果我想让一个任务在后台运行, "dispatch_get_global_queue"队列如何工作?

objective-c - 处理在不同线程上调用 NSOperation completionBlock 这一事实的最佳方法是什么?

c++ - FLTK简单动画