gpt4 book ai didi

ios - 动画期间错误的帧值

转载 作者:可可西里 更新时间:2023-11-01 00:58:37 25 4
gpt4 key购买 nike

我想制作一个类似 iOS 主屏幕文件夹的动画。我在主视图 Controller 中有一个容器 View ,在容器 View 中,我使用动画在两个 View Controller 之间切换。
这是主视图 Controller 类的代码:

class MainContainerViewController: UIViewController {

@IBOutlet weak var containerView: UIView!

let duration = 1.0
var presenting = true
var originFrame = CGRect.zero
var indexPathSelectedCell: IndexPath?

var dismissCompletion: (()->())?


func cycle(fromViewController: UIViewController, toViewController: UIViewController) {

let folderViewC = (presenting ? fromViewController : toViewController) as! ViewController
let projectViewC = (presenting ? toViewController : fromViewController) as! ProjectViewController

let cellView = (presenting ? (folderViewC.folderCollectionView.cellForItem(at: folderViewC.folderCollectionView.indexPathsForSelectedItems!.first!) as! FolderCollectionViewCell).folderView : projectViewC.containerView)!
let cellSnapshot = cellView.snapshotView(afterScreenUpdates: false)!
let cellFrame = containerView.convert(cellView.frame, from: cellView.superview)
cellSnapshot.frame = cellFrame
cellView.isHidden = true

toViewController.view.frame = self.containerView.frame
toViewController.view.layoutIfNeeded()
toViewController.view.alpha = 0


presenting ? (projectViewC.containerView.isHidden = true) : ((folderViewC.folderCollectionView.cellForItem(at: self.indexPathSelectedCell!) as! FolderCollectionViewCell).folderView.isHidden = true)

fromViewController.willMove(toParentViewController: nil)
self.addChildViewController(toViewController)
self.containerView.addSubview(toViewController.view)
self.containerView.addSubview(cellSnapshot)

UIView.animate(withDuration: duration, animations: {


toViewController.view.alpha = 1.0

let finalFrame = self.presenting ? projectViewC.containerView.frame : self.containerView.convert((folderViewC.folderCollectionView.cellForItem(at: self.indexPathSelectedCell!) as! FolderCollectionViewCell).folderView.frame, from: (folderViewC.folderCollectionView.cellForItem(at: self.indexPathSelectedCell!) as! FolderCollectionViewCell).folderView.superview)

cellSnapshot.frame = finalFrame
}) { (_) in

if !self.presenting {
self.dismissCompletion?()
}

self.presenting ? (projectViewC.containerView.isHidden = false) : ((folderViewC.folderCollectionView.cellForItem(at: self.indexPathSelectedCell!) as! FolderCollectionViewCell).folderView.isHidden = false)

cellSnapshot.removeFromSuperview()
fromViewController.view.removeFromSuperview()
fromViewController.removeFromParentViewController()
toViewController.didMove(toParentViewController: nil)

}

}

}

除了 let finalFrame = self.presenting ? projectViewC.containerView.frame : self.containerView.convert((folderViewC.folderCollectionView.cellForItem(at: self.indexPathSelectedCell!) as!FolderCollectionViewCell).folderView.frame, 来自: (folderViewC.folderCollectionView.cellForItem(at: self.indexPathSelectedCell! ) as!FolderCollectionViewCell).folderView.superview)finalFrame 设置为错误的值:

  • 当它呈现时,快照会出现在屏幕中间
  • 当它解散时,Snapshot 会靠近他正确的位置。

let finalFrame = self.presenting ? projectViewC.containerView.frame : self.containerView.convert((folderViewC.folderCollectionView.cellForItem(at: self.indexPathSelectedCell!) as!FolderCollectionViewCell).folderView.frame, 来自: (folderViewC.folderCollectionView.cellForItem(at: self.indexPathSelectedCell! ) as! FolderCollectionViewCell).folderView.superview) 行以这种方式工作:

  • 检查它是否正在呈现
  • 如果它正在呈现,则将常量设置为最终文件夹 View 的frame
  • 如果取消转换所选 Collection View 单元格的 folderView 的 frame 值,则从该 folderView 的父 View 转换

这是整个项目:https://github.com/ale00/OnteamAnon

最佳答案

由于您在 projectViewController 中对 containerView 的大小使用自动布局,因此您需要在这部分代码之后调用 self.containerView 上的 layoutIfNeeded() :

self.addChildViewController(toViewController)
self.containerView.addSubview(toViewController.view)
self.containerView.addSubview(cellSnapshot)

于是变成了:

self.addChildViewController(toViewController)
self.containerView.addSubview(toViewController.view)
self.containerView.addSubview(cellSnapshot)
self.containerView.layoutIfNeeded()

关于ios - 动画期间错误的帧值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39021786/

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