29

NSFetchedResultsController によって設定されている UITableView があります。

テーブルの初期ロードで、セルをアニメーション化したいと思いますが、それよりも少しカスタム アニメーションを実行したいと考えています。

[tableView insertRowsAtIndexPaths:withRowAnimation:];

許可します。具体的には、アニメーションの速度を落として、特定の方法で側面から飛び込ませたいと考えています。UITableViewRowAnimation 定数ではこれを達成できませんでした。Core Animation を使用して、私がやりたいことを正確に行う方法はありますか、またはこの特定のインスタンスで UIKit アニメーションに固執する必要がありますか?

助けてくれてありがとう!

ジョエル

4

4 に答える 4

38

最新のソリューション(2017-12-12)

Swift4.0バージョンのanimateメソッドを追加します。次に、以下のソリューションと同じ方法で実装する必要があります。

func animate() {
    for cell in self.tableView.visibleCells {
        cell.frame = CGRect(x: self.tableView.frame.size.width, y: cell.frame.origin.y, width: cell.frame.size.width, height: cell.frame.size.height)
        UIView.animate(withDuration: 1.0) {
            cell.frame = CGRect(x: 0, y: cell.frame.origin.y, width: cell.frame.size.width, height: cell.frame.size.height)
        }
    }
}

新しいソリューション(2015-09-05)

Swift2.0バージョンのanimateメソッドを追加します。次に、以下のソリューションと同じ方法で実装する必要があります。

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)
        }
    }
}

新しいソリューション(2014-09-28)

実装をより簡単にし、iOS8で動作するように、ソリューションを少し作り直しました。このanimateメソッドをTableViewControllerに追加し、アニメーション化するときはいつでも呼び出すだけです(たとえば、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.height)];
        [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メソッドをオーバーライドして、このメソッドを自分で呼び出す必要があります。

コメント

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];
}
于 2013-01-06T12:25:39.897 に答える
1

これを使って:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
         [cell setFrame:CGRectMake(320, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height)];
            [UIView animateWithDuration:2 animations:^{
                [cell setFrame:CGRectMake(100, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height)];

            }];

}
于 2014-09-26T18:48:20.820 に答える