自定义演示过渡在iOS13上中断

时间:2019-10-25 14:14:59

标签: ios swift

我在全屏宽度的屏幕中央有一个具有小图像的CollectionView和一个具有单个图像的UIViewController。

当用户点击小图像时,它应该缩放以占据整个屏幕宽度。 两者之间有一个自定义过渡动画。

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)
        }
    }
}

控制台日志:

$$$ 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)

它曾经可以正常工作,但是自从iOS 13以来,我注意到关闭动画的开始时图像的宽度超出了应有的范围,尽管帧打印正确的值也关闭了动画,但结束时图像的位置太高了。当前动画的效果很好

1 个答案:

答案 0 :(得分:0)

previewTutorialVC?.tutorialImageView.translatesAutoresizingMaskIntoConstraints = true 

此内部关闭块解决了该问题。如上所述here