菜鸟教程小白 发表于 2022-12-11 22:08:03

ios - 设备旋转后 CGAffineTransform.identity 无法正确重置变换


                                            <p><p>我正在做一个自定义转换,如果在呈现动画之后,设备将被旋转,然后destinationVC 将被关闭,originVC 转换不正确(不完成屏幕)。如果没有设备旋转,一切正常。有没有人可以帮帮我?</p>

<p>这是我展示和关闭动画的代码:</p>

<pre><code>func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
      guard let originViewController = transitionContext.viewController(forKey: .from),
            let destinationViewController = transitionContext.viewController(forKey: .to) else { return }

      destinationViewController.view.transform = CGAffineTransform(translationX: 0, y: destinationViewController.view.frame.height)
      let duration = transitionDuration(using: transitionContext)

      UIView.animate(withDuration: duration, animations: {
            destinationViewController.view.transform = CGAffineTransform(translationX: 0, y: 0)
            originViewController.view.transform = originViewController.view.transform.scaledBy(x: 0.95, y: 0.95)
            originViewController.view.layer.cornerRadius = 8.0
      }, completion: { completed in            
            transitionContext.completeTransition(completed)
      })
    }

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
      guard let originViewController = transitionContext.viewController(forKey: .from),
            let destinationViewController = transitionContext.viewController(forKey: .to) else { return }

      let duration = transitionDuration(using: transitionContext)

      UIView.animate(withDuration: duration, animations: {
            originViewController.view.transform = CGAffineTransform(translationX: 0, y: destinationViewController.view.frame.height)
            destinationViewController.view.transform = CGAffineTransform.identity
            destinationViewController.view.layer.cornerRadius = 0.0
      }, completion: { completed in
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
      })
    }
</code></pre>

<p>屏幕:
之前的动画
<a href="/image/eEAq6.png" rel="noreferrer noopener nofollow"><img src="/image/eEAq6.png" alt="Before present animation"/></a>
现在动画之后
<a href="/image/KVpov.png" rel="noreferrer noopener nofollow"><img src="/image/KVpov.png" alt="After present animation"/></a>
设备旋转后
<a href="/image/gyevA.png" rel="noreferrer noopener nofollow"><img src="/image/gyevA.png" alt="After device rotation"/></a>
关闭动画后
<a href="/image/EqEm3.png" rel="noreferrer noopener nofollow"><img src="/image/EqEm3.png" alt="After dismiss animation"/></a> </p>

<p>编辑:
当我添加 <code>destinationViewController.view.frame = transitionContext.finalFrame(for:destinationViewController)</code> 以关闭动画时,一切似乎都正常,但我不知道这是否正确</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在 ViewC1 中添加一个带有前导、顶部、底部、尾随约束的 subview 。
工作完整代码</p>

<pre><code>class ViewC1: UIViewController {

    @IBAction func presentNow(_ sender: Any) {
      let viewc = storyboard!.instantiateViewController(withIdentifier: &#34;ViewC2&#34;) as! ViewC2
      viewc.transitioningDelegate = viewc
      viewc.modalPresentationStyle = .overCurrentContext
      present(viewc, animated: true, completion: nil)
    }

}

class ViewC2: UIViewController {

    let pres = PresentAnimator()
    let diss = DismissAnimator()

    @IBAction func dissmisNow(_ sender: Any) {
      dismiss(animated: true, completion: nil)
    }
}

extension ViewC2: UIViewControllerTransitioningDelegate {
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -&gt; UIViewControllerAnimatedTransitioning? {
      return pres
    }

    func animationController(forDismissed dismissed: UIViewController) -&gt; UIViewControllerAnimatedTransitioning? {
      return diss
    }
}

class PresentAnimator: NSObject, UIViewControllerAnimatedTransitioning {

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -&gt; TimeInterval {
      return 1.0
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
      guard let originViewController = transitionContext.viewController(forKey: .from),
            let destinationViewController = transitionContext.viewController(forKey: .to) else { return }

      transitionContext.containerView.addSubview(destinationViewController.view)
      destinationViewController.view.frame = transitionContext.containerView.bounds

      let originView = originViewController.view.subviews.first

      destinationViewController.view.transform = CGAffineTransform(translationX: 0, y: destinationViewController.view.frame.height)
      let duration = transitionDuration(using: transitionContext)

      UIView.animate(withDuration: duration, animations: {
            destinationViewController.view.transform = CGAffineTransform(translationX: 0, y: 0)
            originView?.transform = CGAffineTransform(scaleX: 0.95, y: 0.95)
            originView?.layer.cornerRadius = 8.0
      }, completion: { completed in
            transitionContext.completeTransition(completed)
      })
    }
}

class DismissAnimator: NSObject, UIViewControllerAnimatedTransitioning {

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -&gt; TimeInterval {
      return 1.0
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
      guard let originViewController = transitionContext.viewController(forKey: .from),
            let destinationViewController = transitionContext.viewController(forKey: .to) else { return }

      let originView = destinationViewController.view.subviews.first
      let duration = transitionDuration(using: transitionContext)

      UIView.animate(withDuration: duration, animations: {
            originViewController.view.transform = CGAffineTransform(translationX: 0, y: destinationViewController.view.frame.height)
            originView?.transform = CGAffineTransform.identity
            originView?.layer.cornerRadius = 0.0
      }, completion: { completed in
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
      })
    }
}
</code></pre>

<p> <a href="/image/F5O4p.gif" rel="noreferrer noopener nofollow"><img src="/image/F5O4p.gif" alt="enter image description here"/></a> </p>

<p><strong>更新:</strong>
或者如果你不想使用 <code>view.subviews.first</code></p>,你可以覆盖 willRotate 和 didRotate

<pre><code>var prevTrans: CGAffineTransform?
override func willRotate(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval) {
    prevTrans = view.transform
    view.transform = CGAffineTransform.identity
}

override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
    if let prevTrans = prevTrans {
      view.transform = prevTrans
    }
}

</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 设备旋转后 CGAffineTransform.identity 无法正确重置变换,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/54604354/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/54604354/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 设备旋转后 CGAffineTransform.identity 无法正确重置变换