简体   繁体   English

iOS13 上的自定义演示转换中断

[英]Custom Presentation transition broke on iOS13

I have this CollectionView with small images and a UIViewController with single image on the center of a screen with full-screen width.我有这个带有小图像的 CollectionView 和一个 UIViewController,在屏幕中央有一个全屏宽度的单个图像。

When user taps on small image it should scale to take full screen width.当用户点击小图像时,它应该缩放以占据全屏宽度。 There's a custom transition animation between those two.这两者之间有一个自定义转换 animation。

let previewVC = PreviewTutorialViewController(image: image!, imageFrame: frame, text: data!.text, imageView: cell.toDoImageView)
previewVC.modalPresentationStyle = .overCurrentContext
previewVC.transitioningDelegate = previewVC
self.present(previewVC, animated: true, completion: nil)

AnimationController动画控制器

class AnimationController: NSObject, UIViewControllerAnimatedTransitioning {
    var duration = 10.4
    var isPresenting: Bool

    init(forTransitionType type: TransitionType) {
        self.isPresenting = type == .presenting
        super.init()
    }

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return self.duration
    }


    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        let containerView = transitionContext.containerView

        let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!
        let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!

        var imageRectAfter : CGRect = .zero
        var imageRectInitial : CGRect = .zero

        var previewTutorialVC : PreviewTutorialViewController?

        if self.isPresenting {
            containerView.addSubview(toVC.view)
            containerView.layoutIfNeeded()
            previewTutorialVC = (toVC as! PreviewTutorialViewController)
            imageRectInitial = previewTutorialVC!.initialRect
            imageRectAfter = previewTutorialVC!.tutorialImageView.frame
            previewTutorialVC?.initialImageView?.alpha = 0
            print("$$$ animating image view frame : PRESENTING ", imageRectInitial, " to ", imageRectAfter)
        } else {
            previewTutorialVC = (fromVC as! PreviewTutorialViewController)
            imageRectAfter = previewTutorialVC!.initialRect
            imageRectInitial = previewTutorialVC!.tutorialImageView.frame
            print("$$$ animating image view frame : DISSMISSING ", imageRectInitial, " to ", imageRectAfter)
            //let frame2 = previewTutorialVC!.tutorialImageView.convert(imageRectInitial, to: containerView)
            //imageRectInitial = CGRect(x: 0, y: 430, width: 414, height: 155)
            //print("$$$ converted :", frame2)
            print("$$$ ", containerView.bounds.size.width, previewTutorialVC?.view.bounds.size.width)
        }

        previewTutorialVC?.tutorialImageView.transform = .identity

        previewTutorialVC?.tutorialImageView.frame = imageRectInitial
        previewTutorialVC?.containerView.alpha = isPresenting == true ? 0 : 1
        previewTutorialVC?.closeButton.alpha = self.isPresenting == true ? 0 : 1

        //previewTutorialVC?.textContainerView.transform = isPresenting == true ? CGAffineTransform(translationX: 0, y: previewTutorialVC?.textContainerView.frame.size.height ?? 0) : .identity

        UIView.animate(withDuration: duration, delay: 0, options: [.curveEaseOut], animations: {
            previewTutorialVC?.tutorialImageView.frame = imageRectAfter
            previewTutorialVC?.containerView.alpha = self.isPresenting == true ? 1 : 0
            previewTutorialVC?.closeButton.alpha = self.isPresenting == true ? 1 : 0
       //     previewTutorialVC?.textContainerView.transform = self.isPresenting == true ? .identity : CGAffineTransform(translationX: 0, y: previewTutorialVC?.textContainerView.frame.size.height ?? 0)
        }) { (_) in
            if self.isPresenting == false { previewTutorialVC?.initialImageView?.alpha = 1 }
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
        }
    }
}

Console log:控制台日志:

$$$ animating image view frame : PRESENTING  (15.0, 461.5, 384.0, 143.5)  to  (0.0, 370.5, 414.0, 155.0)
$$$ animating image view frame : DISSMISSING  (0.0, 370.5, 414.0, 155.0)  to  (15.0, 461.5, 384.0, 143.5)

It used to work fine, but since iOS 13 i've noticed dismiss animation starts with way too wide image than it should be, although frame prints correct values.. also dismiss animation ends with image being a bit too high.它曾经工作正常,但自从 iOS 13 以来,我注意到驳回 animation 开始时的图像比应有的宽度太宽,尽管框架打印正确的值.. 也驳回 animation 以过高的图像结束Present animation's working fine当前动画工作正常

previewTutorialVC?.tutorialImageView.translatesAutoresizingMaskIntoConstraints = true 

this inside dismiss block fixed the issue.这个内部dismiss块解决了这个问题。 As mentioned here正如这里提到的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM