gpt4 book ai didi

ios - 无法获得单元格内图像相对于 super View 的正确位置

转载 作者:行者123 更新时间:2023-11-29 00:23:10 24 4
gpt4 key购买 nike

我有一个包含图像的 Collection View 。我尝试做的是从单元格中获取图像的位置,并将其放置在与点击 collectionView 单元格时完全相同的位置。

这是我最初的 Collection View :

enter image description here

这是点击后图像的位置:

enter image description here

代码:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.item != 0 {
guard let cell = collectionView.cellForItem(at: indexPath) as? CategoryCell else { return }
selectedImageRect = cell.image.convert(cell.image.frame, to: self.collectionView)
selectedImage = cell.image.image!
}
}

//////

class CategoryAnimator: NSObject, UIViewControllerAnimatedTransitioning {

var selectedImage: UIImage?
var selectedImageRect: CGRect?

func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 12.0
}

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!
let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!
let finalFrameForVC = transitionContext.finalFrame(for: toViewController)
let containerView = transitionContext.containerView
let bounds = UIScreen.main.bounds
toViewController.view.frame = finalFrameForVC.offsetBy(dx: 0, dy: -bounds.size.height)
containerView.addSubview(toViewController.view)

var passedImage = UIImageView(frame: selectedImageRect!)
passedImage.image = selectedImage!
fromViewController.view.addSubview(passedImage)

UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0, options: .curveLinear, animations: {
fromViewController.view.alpha = 0.5
toViewController.view.frame = finalFrameForVC

}, completion: {
finished in
transitionContext.completeTransition(true)
fromViewController.view.alpha = 1.0
})
}
}

我做错了什么?

最佳答案

    selectedImageRect = cell.convert(cell.image.frame, to: self.view)

应该可以了

关于ios - 无法获得单元格内图像相对于 super View 的正确位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43805800/

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