最新のソリューション(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];
}