2017-05-21 31 views
0

我正在尝试模仿将图像从全屏缩小到其缩略图的iPhoto自定义动画。其中一个怪癖是,如果您以全屏模式打开图像,然后滑动到足以让您正在查看的图像的相关缩略图在父视图的集合视图中不可见,则需要将其滚动到可见的之前解散模态。这是这样的图像有一个缩略图来动画。滚动父视图的CollectionView以显示模式解除之前的单元格

我打电话给我的动画师做了这个,但是当后续调用碰巧检索可见单元格的imageview时,它崩溃了,因为集合视图还没有滚动。我尝试过实施延迟,但没有帮助。

// controller with the collection view  
guard let toVC = transitionContext.viewController(forKey: .to) as? GalleryViewController else { return } 

// controller with image 
guard let fromVC = transitionContext.viewController(forKey: .from) as? PhotoViewController else { return } 

// this performs the scroll; implementation below 
toVC.scrollToShow(index: fromVC.selectedIndex) 

// this is where it crashes, as the cell doesn't exist yet 
guard let toImageView = delegate?.imageView(for: fromVC.selectedIndex) else { return } 

而用于显示电池代码:

func scrollToShow(index: Int) { 
    let indexPath = IndexPath(row: index, section: 0) 
    if collectionView.cellForItem(at: indexPath) == nil { 
    // only scroll the cell to the top if it isn't visible 
    collectionView.scrollToItem(at: indexPath, at: .top, animated: false) 
    } 
} 

最后,当它崩溃,委托方法:

func imageView(for selectedIndex: Int) -> UIImageView { 
    let cell = collectionView.cellForItem(at: IndexPath(row: selectedIndex, section: 0)) as! GalleryCell 
    return cell.imageView 
} 

任何帮助表示赞赏!

+0

你能说出你得到的错误吗?此外,当用户在显示大图像视图后点击单元格时,您可以滚动缩略图视图。这样,当用户关闭大图像视图,你不滚动,因为滚动已经完成 – user1046037

+0

是啊,这只是一个'致命的错误:意外地发现零,而试图获取cellForItem时解包可选值'。 –

回答

0

我想通了。在崩溃的时候,我po collectionView'd,并注意到contentOffset是它应该是,但cellForItemAt:没有被调用。所以我试图强制collectionView来布局:

... 
collectionView.scrollToItem(at: indexPath, at: .top, animated: false) 
collectionView.setNeedsLayout() 
collectionView.layoutIfNeeded() 
... 

而这个工作。我想iOS会进行优化,只会在下一个布局周期中更新它。

相关问题