containerView的自定义动画中的modalPresentationStyle .overCurrentContext返回黑屏

时间:2018-06-17 10:11:46

标签: ios swift xcode uimodalpresentationstyle

我在另一个viewController的containerView中有一个tableViewController。

点击图像时,会向另一个viewController发生自定义转换以查看图像。过渡效果很好。

当自定义转换发生时,虽然在容器视图的tableViewController上调用viewWillDisappear,然后在显示的viewController被解除时再次viewWillAppear(再次转换)。

这会在tableView中偶尔导致一些异常滚动。

我想要的是为imageViewer提供一个转换但是overCurrentContext。

我尝试过人们建议的内容如下:

self.definesPresentationContext = true
vc.modalPresentationStyle = .overCurrentContext
vc.transitioningDelegate = self
self.present(vc, animated: true, completion: nil)

不幸的是,这不起作用,当imageViewController被解除时,最初转换回来是正确的,可以看到tableViewController。但是一旦转换完成,视图就会变黑。

我认为这可能与我使用containerView的事实有关。但不确定并且不确定如何解决这个问题。

这是我使用的动画制作方法:

private var image: UIImage?
private var fromDelegate: ImageTransitionProtocol?
private var toDelegate: ImageTransitionProtocol?

// MARK: Setup Methods

func setupImageTransition(image: UIImage, fromDelegate: ImageTransitionProtocol, toDelegate: ImageTransitionProtocol) {
    self.image = image
    self.fromDelegate = fromDelegate
    self.toDelegate = toDelegate

}

// MARK: UIViewControllerAnimatedTransitioning

// 1: Set animation speed
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
    return 0.4
}

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

        let containerView = transitionContext.containerView
        // 2: Get view controllers involved
        let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!
        let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!

        // 3: Set the destination view controllers frame
        toVC.view.frame = fromVC.view.frame

        // 4: Create transition image view
        let imageView = UIImageView(image: image)
        imageView.contentMode = .scaleAspectFill
        imageView.frame = (fromDelegate == nil) ? CGRect() : fromDelegate!.imageWindowFrame()
        imageView.clipsToBounds = true
        containerView.addSubview(imageView)

        // 5: Create from screen snapshot

        fromDelegate!.transitionSetup()
        toDelegate!.transitionSetup()
        let fromSnapshot = fromVC.view.snapshotView(afterScreenUpdates: true)!
        fromSnapshot.frame = fromVC.view.frame
        containerView.addSubview(fromSnapshot)

        // 6: Create to screen snapshot
        let toSnapshot = toVC.view.snapshotView(afterScreenUpdates: true)!
        toSnapshot.frame = fromVC.view.frame
        containerView.addSubview(toSnapshot)
        toSnapshot.alpha = 0

        // 7: Bring the image view to the front and get the final frame
        containerView.bringSubview(toFront: imageView)
        let toFrame = (self.toDelegate == nil) ? CGRect() : self.toDelegate!.imageWindowFrame()

        // 8: Animate change
        UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.8, options: .curveEaseOut, animations: {
            toSnapshot.alpha = 1
            imageView.frame = toFrame

        }, completion:{ [weak self] (finished) in
            self?.toDelegate!.transitionCleanup()
            self?.fromDelegate!.transitionCleanup()
            // 9: Remove transition views
            imageView.removeFromSuperview()
            fromSnapshot.removeFromSuperview()
            toSnapshot.removeFromSuperview()

            // 10: Complete transition
            if !transitionContext.transitionWasCancelled {
                containerView.addSubview(toVC.view)
            }
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
        })
    }

这是在呈现viewController:

func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        let imagePresentationViewController = (presented as! UINavigationController).topViewController as! ImagePresentationViewController
        self.transition.setupImageTransition( image: pickedImageForTransition!, imageType: nil,
                                              fromDelegate: self,
                                              toDelegate: imagePresentationViewController)
        return transition
    }

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        let imagePresentationViewController = (dismissed as! UINavigationController).topViewController as! ImagePresentationViewController
        transition.setupImageTransition( image: pickedImageForTransition!, imageType: .listsImage,
                                         fromDelegate: imagePresentationViewController,
                                         toDelegate: self)
        return transition
    }

0 个答案:

没有答案