2016-07-13 125 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