动画帧改变一个UITableViewCell子视图无需重新加载的最佳方式 [英] Best way to animate frame change in a UITableViewCell subview WITHOUT reloading

查看:173
本文介绍了动画帧改变一个UITableViewCell子视图无需重新加载的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有动画在一个UITableViewCell子视图帧变化的最佳实践,但没有重装电池(通过 reloadData 或通过重新加载特定的细胞和部分)。

Is there a best practice for animating a frame change in a UITableViewCell subview, but WITHOUT reloading the cell (either by reloadData or by reloading specific cells and sections).

例子:我有一个简单的 UIView的为自定义UITableViewCell ,的子视图当子视图占有的%总排的宽度。我想动画改变这个宽度的UIView (而容器来看,UITableViewCell的内容来看,保持不变),但没有重装,因为重装动画选项唐'T显示的子视图中我喜欢的方式顺利大小的改变框架。

Example: I have a simple UIView as a subview of a custom UITableViewCell, where the subview occupies some % of the total row width. I want to animate changes to the width of this UIView (while the container view, the UITableViewCell content view, stays the same), but without reloading, because the reloading animation options don't show the frame of the subview smoothly changing size in the way I'd like.

我最初的解决方案是通过可见的单元格进行迭代,并用手工将的UIView 动画块,更改每帧例如

My initial solution was to iterate through the visible cells, and change each frame manually using a UIView animation block, e.g.

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的解决显卡扭结;在所有的细胞似乎迭代矫枉过正。

but while this seems to work on iOS7, is leading to some funky and difficult to troubleshoot graphics kinks on iOS6; iterating through all the cells seems overkill.

为做到这一点的最好办法任何建议?

Any recommendations for the best way to do this?

推荐答案

您可以使用NSNotificationCenter张贴通知,告知每个单元更改特定视图的宽度。

You can use the NSNotificationCenter to post a notification to inform each cell to change the width of that specific view.

当创建一个单元格可以将其注册到一个特定的通知:

When a cell is created you can register it to a specific notification:

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

然后就可以在每个单元处理的通知,让你需要的更改:

Then you can handle in each cell the notification and make the changes you need:

- (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
 }];
}

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

When you want to change the width you only need to post the event.

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

不要忘了重复使用的每个细胞,当细胞被释放确保您注销该事件。

Do not forget to reuse each cell and when the cell is deallocated make sure you unregister that event.

[[NSNotificationCenter defaultCenter] removeObserver:self]; 

这篇关于动画帧改变一个UITableViewCell子视图无需重新加载的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆