我有一个带有图像的集合视图。我尝试做的是从单元格中获取图像的位置,并将其放置在tapView单元格被轻敲的位置。
这是我最初的collectionView:
这是点击后的图像位置:
代码:
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
})
}
}
我做错了什么?
答案 0 :(得分:1)
selectedImageRect = cell.convert(cell.image.frame, to: self.view)
应该这样做