gpt4 book ai didi

ios - 如何在自定义动画中访问 toView 对象的框架

转载 作者:搜寻专家 更新时间:2023-10-31 22:59:48 26 4
gpt4 key购买 nike

我想做一个类似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))

我关注了this教程,用 Swift 编写我的代码

那么,如何才能正确访问该属性呢?

最佳答案

框架是零矩形,因为在您访问它时, View 的布局传递尚未发生。设置完 toVC.view 框架后,添加这一行:

toVC.view.layoutIfNeeded()

我写过关于在 View Controller 转换中使用快照的文章 here如果你有兴趣。

关于ios - 如何在自定义动画中访问 toView 对象的框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38346254/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com