如何在自定义动画中访问toView对象的frame

4

我想制作一个类似于iOS主屏幕文件夹的动画,并且需要知道“文件夹”的最终位置的帧数:screen capture of the final view

我正在使用自定义转换,以下是动画文件的代码:

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))之外,其他都正确无误。
我按照这篇教程进行编写,使用Swift语言。
那么,我该如何正确访问此属性呢?
1个回答

1

框架是零矩形,因为在您访问它的时候,视图的布局尚未发生。在设置toVC.view框架后,添加此行:

toVC.view.layoutIfNeeded()

如果您感兴趣,我已经在这里写过有关视图控制器转换中快照使用的内容。


我想访问 toViewC.containerView.frame,也就是“文件夹”的矩形框,正如您在截图中所看到的。我已经删除了转换,但它仍然无法工作... - alessionossa
但是如果我向 completeTransition 发送 false,我会返回到我的第一个视图。 - alessionossa
唉,我在这个答案上做得很糟糕,对不起。我之前的回答也是错误的。 - jrturton

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接