你能为UITableView单元格插入做自定义动画吗? [英] Can you do custom animations for UITableView Cell Inserts?

查看:1100
本文介绍了你能为UITableView单元格插入做自定义动画吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableView由一个NSFetchedResultsController填充。



在表的初始加载我想单元格动画,但我想要做一些更多的自定义动画

  [tableView insertRowsAtIndexPaths:withRowAnimation:]; 

允许。具体来说,我想减慢动画,让他们从侧面以特定的方式飞行。我还没有能够通过UITableViewRowAnimation常量来实现这一点。有没有办法使用核心动画来做我想要的,或者我需要坚持UIKit动画在这个特定的实例?



感谢任何帮助! p>

Joel

解决方案

NEWER解决方案)



添加一个Swift 2.0版本的animate方法。然后应该按照下面的解决方法实现:

  func animate(){
self.tableView.visibleCells {
cell.frame = CGRectMake(320,cell.frame.origin.y,cell.frame.size.width,cell.frame.size.height)
UIView.animateWithDuration( 1.0){
cell.frame = CGRectMake(0,cell.frame.origin.y,cell.frame.size.width,cell.frame.size.height)
}
}
}

新解决方案(2014-09-28) / p>

我重新设计了解决方案,使得实现更容易,并使其与iOS8一起工作。所有你需要做的是在你的TableViewController中添加这个 animate 方法,并且当你想要它动画时调用它(例如,在你的reload方法,但你可以调用它

   - (void)animate 
{
[self.tableView visibleCells] enumerateObjectsUsingBlock:^(UITableViewCell * cell,NSUInteger idx,BOOL * stop){
[cell setFrame:CGRectMake(320,cell.frame.origin.y,cell.frame.size.width,cell.frame.size。高度)];
[UIView animateWithDuration:1 animations:^ {
[cell setFrame:CGRectMake(0,cell.frame.origin.y,cell.frame.size.width,cell.frame.size.height)] ;
}];
}];
}

再次,改变动画的样子。



旧解决方案(2013-06-06)

>

您可以通过实现自己的UITableView并覆盖insertRowsAtIndexPaths方法来实现。这里是一个例子,如何可以看起来像细胞从右边推,真的很慢(1秒动画):

   - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation 
{
for(NSIndexPath * indexPath in indexPaths)
{
UITableViewCell * cell = self cellForRowAtIndexPath:indexPath];
[cell setFrame:CGRectMake(320,cell.frame.origin.y,cell.frame.size.width,cell.frame.size.height)];

[UIView beginAnimations:NULL context:nil];
[UIView setAnimationDuration:1];
[cell setFrame:CGRectMake(0,cell.frame.origin.y,cell.frame.size.width,cell.frame.size.height)];
[UIView commitAnimations];
}
}

您可以自己玩动画。此方法不会由表视图自动调用,因此您必须覆盖表视图委托中的reloadData方法,并自己调用此方法。



COMMENT



reloadData方法应该如下所示:

   - (void)reloadData 
{
[super reloadData];
NSMutableArray * indexPaths = [[NSMutableArray alloc] init];
for(int i = 0; i <[_data count]; i ++)
[indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
[self insertRowsAtIndexPaths:indexPaths withRowAnimation:0];
}


I've got a UITableView that is being populated by an NSFetchedResultsController.

On the initial load of the table I would like the Cells to be animated in but I would like to do a little bit more custom animation than

[tableView insertRowsAtIndexPaths:withRowAnimation:];

Allows. Specifically I would like to slow down the animations and have them fly in from the side in a specific way. I have not been able to achieve this by UITableViewRowAnimation constants. Is there a way to use Core Animation to do exactly what I want or do I need to stick with UIKit animations in this specific instance?

Thanks for any help!

Joel

解决方案

NEWER SOLUTION (2015-09-05)

Adding a Swift 2.0 version of the animate method. It should then be implemented in the same way as the solution below:

func animate() {
    for cell in self.tableView.visibleCells {
        cell.frame = CGRectMake(320, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height)
        UIView.animateWithDuration(1.0) {
            cell.frame = CGRectMake(0, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height)
        }
    }
}

NEW SOLUTION (2014-09-28)

I reworked the solution a bit to make the implementation more easier and to make it work with iOS8. All you need to do is to add this animate method in your TableViewController and call it whenever you want it to animate (for instance, in your reload method, but you could call it at any time):

- (void)animate
{
    [[self.tableView visibleCells] enumerateObjectsUsingBlock:^(UITableViewCell *cell, NSUInteger idx, BOOL *stop) {
        [cell setFrame:CGRectMake(320, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height)];
        [UIView animateWithDuration:1 animations:^{
            [cell setFrame:CGRectMake(0, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height)];
        }];
    }];
}

Again, change the animation how you like. This particular code will animate the cells in from the right at a slow rate.

OLD SOLUTION (2013-06-06)

You can do this by implementing your own UITableView and overriding the insertRowsAtIndexPaths method. Here is an example of how that could look like where the cells will be pushed from the right, really slowly (1 second animation):

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
{
    for (NSIndexPath *indexPath in indexPaths)
    {
        UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath];
        [cell setFrame:CGRectMake(320, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height)];

        [UIView beginAnimations:NULL context:nil];
        [UIView setAnimationDuration:1];
        [cell setFrame:CGRectMake(0, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height)];
        [UIView commitAnimations];
    }
}

You can play around with the animations yourself. This method will not be called automatically by the table view so you have to override the reloadData method in your table view delegate and call this method yourself.

COMMENT

The reloadData method should look something like this:

- (void)reloadData
{
    [super reloadData];
    NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
    for (int i = 0; i < [_data count]; i++)
        [indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
    [self insertRowsAtIndexPaths:indexPaths withRowAnimation:0];
}

这篇关于你能为UITableView单元格插入做自定义动画吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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