我有一个名为 ProfileCollectionViewController 的集合视图,用于收集图像。单击图像时,它会显示一个 HorizontalProfileViewController,它会全屏显示图像。在 HorizontalProfileViewController 上按下后退按钮时,我希望全屏图像动画回 ProfileViewController 中的缩略图。我将 ProfileViewController 中的选定索引路径作为 initialIndexPath 传递给 HorizontalProfileViewController,以便知道缩略图的位置。下面是我的过渡动画代码
import UIKit
class SEHorizontalFeedToProfile: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return 0.2
}
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
if let profileVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) as? SEProfileGridViewController, horizontalFeedVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) as? SEProfileHorizontalViewController, containerView = transitionContext.containerView() {
let duration = transitionDuration(transitionContext)
profileVC.collectionView?.reloadData()
if let indexPath = horizontalFeedVC.initialIndexPath {
let cell = profileVC.collectionView?.cellForItemAtIndexPath(indexPath)
print(indexPath)
let imageSnapshot = horizontalFeedVC.view.snapshotViewAfterScreenUpdates(false)
let snapshotFrame = containerView.convertRect(horizontalFeedVC.view.frame, fromView: horizontalFeedVC.view)
imageSnapshot.frame = snapshotFrame
horizontalFeedVC.view.hidden = true
profileVC.view.frame = transitionContext.finalFrameForViewController(profileVC)
containerView.insertSubview(profileVC.view, belowSubview: horizontalFeedVC.view)
containerView.addSubview(imageSnapshot)
UIView.animateWithDuration(duration, animations: {
var cellFrame = CGRectMake(0, 0, 0, 0)
if let theFrame = cell?.frame {
cellFrame = theFrame
}
let frame = containerView.convertRect(cellFrame, fromView: profileVC.collectionView)
imageSnapshot.frame = frame
}, completion: { (succeed) in
if succeed {
horizontalFeedVC.view.hidden = false
// cell.contentView.hidden = false
imageSnapshot.removeFromSuperview()
transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
}
})
}
}
}
}
我放了断点,发现在代码中
let cell = profileVC.collectionView?.cellForItemAtIndexPath(indexPath)
单元格为零。我不明白为什么它会为零。请帮我。我提前谢谢你。
profileVC 是 UICollectionViewController 的子类
PS:请查看以下项目,该项目完全相同,没有任何问题。我试图模仿它,但它对我的不起作用。 https://github.com/PeteC/InteractiveViewControllerTransitions