2016-07-13 40 views
3

我要讓像iOS主屏幕上的文件夾設置動畫的訪問框架,我怎麼也得知道的「文件夾」的最終位置的框架:screen capture of the final view在自定義動畫toView對象

我使用自定義的過渡,這裏的動畫文件的代碼:

class FolderAnimationController: NSObject, UIViewControllerAnimatedTransitioning { 

    let duration = 5.0 
    var presenting = true 

    func transitionDuration(_ transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { 
     return duration 
    } 

    func animateTransition(_ transitionContext: UIViewControllerContextTransitioning) { 

     let containerView = transitionContext.containerView() 
     let toViewC = transitionContext.viewController(forKey: UITransitionContextToViewControllerKey)! as! ProjectViewController 
     let fromViewC = transitionContext.viewController(forKey: UITransitionContextFromViewControllerKey)!as! ViewController 

     let cell : FolderCollectionViewCell = fromViewC.folderCollectionView.cellForItem(at: (fromViewC.folderCollectionView.indexPathsForSelectedItems()?.first!)!) as! FolderCollectionViewCell 
     let cellSnapshot: UIView = cell.snapshotView(afterScreenUpdates: false)! 
     let cellFrame = containerView.convert(cell.frame, from: cell.superview) 
     cellSnapshot.frame = cellFrame 
     cell.isHidden = true 

     toViewC.view.frame = transitionContext.finalFrame(for: toViewC) 
     toViewC.view.alpha = 0 
     containerView.addSubview(toViewC.view) 
     containerView.addSubview(cellSnapshot) 

     UIView.animate(withDuration: duration, animations: { 
      toViewC.view.alpha = 1.0 

      let finalFrame = containerView.convert(toViewC.containerView.frame, from: toViewC.view) 
      cellSnapshot.frame = finalFrame 
      }) { (_) in 
       cellSnapshot.removeFromSuperview() 
       transitionContext.completeTransition(true) 
     } 

    } 
} 

所有工作正常,除了let finalFrame = containerView.convert(toViewC.containerView.frame, from: toViewC.view)稱定finalFrame值(是的CGRect變量)(origin = (x = 0, y = 0), size = (width = 0, height = 0))

我已經按照this教程中,斯威夫特

所以寫我的代碼,我怎麼能正確地訪問屬性?

回答

1

該框架是零矩形,因爲在您訪問它的點上,視圖的佈局過程沒有發生。設置好後toVC.view框架,加入這一行:

toVC.view.layoutIfNeeded() 

我已經寫在視圖控制器過渡here如果你有興趣的使用快照。

+0

我想訪問'toViewC.containerView.frame',這是「文件夾」的矩形,正如你可以看到我的截圖一樣。我已經刪除了轉換,但它不工作... – ale00

+0

哦,對不起!我誤讀了。等一下,我會編輯我的答案 – jrturton

+0

但是,如果我發送'false'到'completeTransition'我回到我的第一個視圖 – ale00