Первое, что я бы попробовал, это заменить animate(withDuration:delay:options:animations:completion:)
на performBatchUpdates(_:completion:)
func controller(controller: NSFetchedResultsController,
didChangeObject anObject: AnyObject,
atIndexPath indexPath: NSIndexPath?,
forChangeType type: NSFetchedResultsChangeType,
newIndexPath: NSIndexPath?)
{
if type == NSFetchedResultsChangeType.Insert {
println("Insert Object: \(newIndexPath)")
self.collectionView?.performBatchUpdates({
self.collectionView?.insertItems(at: [newIndexPath])
}, completion: { finished in
self.collectionView?.scrollToItem(at: newIndexPath, at: .centeredHorizontally, animated: true)
})
…
}
Если это все еще вызывает проблемы, вы можете вызвать прокрутку в следующем цикле выполнения.
func controller(controller: NSFetchedResultsController,
didChangeObject anObject: AnyObject,
atIndexPath indexPath: NSIndexPath?,
forChangeType type: NSFetchedResultsChangeType,
newIndexPath: NSIndexPath?)
{
if type == NSFetchedResultsChangeType.Insert {
println("Insert Object: \(newIndexPath)")
self.collectionView?.performBatchUpdates({
self.collectionView?.insertItems(at: [newIndexPath])
}, completion: { finished in
DispatchQueue.main.async { // Defer to next runlop.
self.collectionView?.scrollToItem(at: newIndexPath, at: .centeredHorizontally, animated: true)
}
})
…
}
Наконец, вы можете попробовать только анимацию для прокрутки части.
func controller(controller: NSFetchedResultsController,
didChangeObject anObject: AnyObject,
atIndexPath indexPath: NSIndexPath?,
forChangeType type: NSFetchedResultsChangeType,
newIndexPath: NSIndexPath?)
{
if type == NSFetchedResultsChangeType.Insert {
println("Insert Object: \(newIndexPath)")
self.collectionView?.reloadItems(at: [newIndexPath]) // reload without animating.
DispatchQueue.main.async { // Defer to next runlop.
self.collectionView?.scrollToItem(at: newIndexPath, at: .centeredHorizontally, animated: true)
}
…
}