2016-05-04 8 views
1

私は、画像のコレクションのためのProfileCollectionViewControllerというコレクションビューを持っています。 イメージをクリックすると、イメージをフルスクリーンで表示するHorizo​​ntalProfileViewControllerが表示されます。 Backボタンが押されたときHorizo​​ntalProfileViewController私は全画面イメージをProfileViewControllerのサムネイルにアニメーション表示させたい。サムネイルの位置がわかるように、ProfileViewControllerから選択したインデックスパスをinitialIndexPathとしてHorizo​​ntalProfileViewControllerに渡します。以下は私のトランジションアニメーションコードUICollectionViewControllerへのカスタムポップアニメーションが動作しない

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) 

にセルがnilであることが分かっています。なぜそれが無くなるのか分かりません。私を助けてください。私は事前に感謝します。

profileVCが

UICollectionViewController

のサブクラスであるPS:何の問題もなく、まったく同じことを行い、次のプロジェクトをチェックアウトしてください。私はそれを模倣しようとしましたが、それは私の上で動作しません。 https://github.com/PeteC/InteractiveViewControllerTransitions

+0

nilをすべきではない 'cellForItemAtIndexPath'はnilを返します。あなたの質問はなぜ私の細胞が見えないのですか? – beyowulf

+0

私の質問はなぜpopViewアニメーションのtoViewControllerのセルがないのですか?どうすれば修正できますか? – Subash

+0

コレクションビュー – jrturton

答えて

0

質問のコメントで指摘したように、セルは表示されないためセルはnilに設定されています。あなたのHorizo​​ntalProfileViewControllerは他のピクチャへのスクロールを可能にするので、ProfileCollectionViewControllerにUICollectionViewCellがまだ作成されていないイメージにスクロールすることは可能です。

これを解決するには、現在の画像がUICollectionViewのどこに表示されるべきかをスクロールします。最初にinitialIndexPathが最初に選択された画像のindexPathであるとします。ユーザーが異なるイメージにスクロールして画面上のイメージのindexPathを正確に表すように更新すると仮定すると、UICollectionViewを使用して必要に応じて現在のセルにスクロールさせることができます。これはあなたの目的の効果与えるべきであるあなたのコードにいくつかの調整で

:今

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() //Remove this 

     if let indexPath = horizontalFeedVC.initialIndexPath { 
      var cell = profileVC.collectionView?.cellForItemAtIndexPath(indexPath) //make cell a var so it can be reassigned if necessary 
      print(indexPath) 

      if cell == nil{ //This if block is added. Again it is crucial that initialIndexPath was updated as the user scrolled. 
       profileVC.collectionView?.scrollToItemAtIndexPath(horizontalFeedVC.initialIndexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredVertically, animated: false) 
       profileVC.collectionView?.layoutIfNeeded() //This is necessary to force the collectionView to layout any necessary new cells after scrolling 
       cell = profileVC.collectionView?.cellForItemAtIndexPath(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()) 
        } 
      }) 
     } 
    } 
} 

すると、現在のUICollectionViewCellはまだそれを作成されていない場合:

  1. スクロールのindexPathので、セルはビューの中央に配置されます

  2. UICollectionViewに、必要なUITableViewCellを作成するサブビューをレイアウトするように強制します。

  3. 再度のUITableViewCellを要求しますが、セルが表示されていないか、indexPathが範囲外の場合、この時間は、それが

関連する問題