【问题标题】:Best way to animate frame change in a UITableViewCell subview WITHOUT reloading在不重新加载的情况下在 UITableViewCell 子视图中动画帧更改的最佳方法
【发布时间】:2013-09-25 22:24:56
【问题描述】:

是否有在 UITableViewCell 子视图中为帧更改设置动画的最佳做法,但不重新加载单元格(通过reloadData 或通过重新加载特定单元格和部分)。

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

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

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 上的图形问题出现一些时髦且难以解决的问题;遍历所有单元格似乎有点过头了。

对于最好的方法有什么建议吗?

【问题讨论】:

  • 我不明白你的意思。子视图需要独立于容器视图的大小进行更改(即UITableViewCell 内容视图 - 始终保持固定)。自动布局有何帮助?

标签: ios objective-c animation uitableview subview


【解决方案1】:

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

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

[[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]; 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 2014-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多