我会使用CATransaction
得到通知时UITableView
成品动画:
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[CATransaction begin];
[CATransaction setCompletionBlock:^{
if(self.shouldScrollToTop) {
[self.tableView setContentOffset:CGPointZero animated:YES];
}
self.shouldScrollToTop = NO;
}];
[self.tableView beginUpdates];
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
[CATransaction commit];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertRowsAtIndexPaths:@[ newIndexPath ] withRowAnimation:UITableViewRowAnimationAutomatic];
self.shouldScrollToTop = YES;
break;
}
}
这是很酷。我唯一的问题是在不同的代码块中有CATransaction块。我可能在别处也有其他CATransition块。 – p0lAris
@ p0lAris你可以尝试在CATransaction中包装endUpdates。在调用endUpdates之前,我认为表视图不会执行任何操作。此外,CAT交易可以嵌套我认为,所以即使你有其他交易,你应该是安全的。 – Andy