Swift 3 /为什么来自snapshotView的快照在iPhone 7 Plus Simulator中显示为空白?

Dav*_*eek 6 ios swift

我正在使用以下代码创建自定义菜单转换.我花了最后两个小时试图弄清楚为什么快照显示为空白(仅使用iPhone 7 Plus模拟器).但是当我想创建一个视频使其成为这个线程的gif时,它可以在我的iPhone 6S Plus上运行.

更新:适用于iPhone 6S模拟器.但仍然没有在7 Plus.

import UIKit

class PresentMenuAnimator : NSObject {
}

extension PresentMenuAnimator : UIViewControllerAnimatedTransitioning {
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.6
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

        let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)
        let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
        let containerView = transitionContext.containerView
        containerView.insertSubview((toVC?.view)!, belowSubview: (fromVC?.view)!)

        // replace main view with snapshot
        let snapshot = fromVC?.view.snapshotView(afterScreenUpdates: false)
        snapshot?.tag = MenuHelper.snapshotNumber
        snapshot?.isUserInteractionEnabled = false
        snapshot?.layer.shadowOpacity = 0.7
        containerView.insertSubview(snapshot!, aboveSubview: (toVC?.view)!)
        fromVC?.view.isHidden = true

        UIView.animate(
            withDuration: transitionDuration(using: transitionContext),
            animations: {
                snapshot!.center.x += UIScreen.main.bounds.width * MenuHelper.menuWidth
            },
            completion: { _ in
                fromVC?.view.isHidden = false
                transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
            }
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

iPhone 6S Plus(物理设备)iOS 10

在此输入图像描述

iPhone 7 Plus(模拟器)iOS 10

在此输入图像描述

在此输入图像描述

为什么模拟器上的快照是空白的?

GitHub测试项目

Zai*_*han 7

添加此UIView扩展,

public extension UIView {
    public func snapshotImage() -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(bounds.size, isOpaque, 0)
        drawHierarchy(in: bounds, afterScreenUpdates: false)
        let snapshotImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return snapshotImage
    }

    public func snapshotView() -> UIView? {
        if let snapshotImage = snapshotImage() {
            return UIImageView(image: snapshotImage)
        } else {
            return nil
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

更新以下代码,

let snapshot = fromVC?.view.snapshotView(afterScreenUpdates: false)
Run Code Online (Sandbox Code Playgroud)

用,

let snapshot = fromVC?.view.snapshotView()
Run Code Online (Sandbox Code Playgroud)

参考

  • 这并没有完全回答为什么snapshotview(afterScreeUpdates :)不起作用的问题,只提供了另一种解决方案.你知道为什么这个方法不起作用吗?我遇到了同样的问题 (5认同)
  • 谢谢@Zaid,但是你知道为什么它与snapshotView(afterScreenUpdates :)有如此不同吗??? 了解更多信息会很棒 (2认同)