Swift 3 / Why is the snapshot from the snapshot displayed in the iPhone 7 Plus Simulator?

I use the following code to create a custom menu transition. For the past two hours, I have been trying to find out why the picture looks blank (only using the iPhone 7 Plus simulator). But when I wanted to create a video to turn it into a gif for this stream, it worked on my iPhone 6S Plus.

Update: Works on iPhone 6S Simulator. But still not in 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) } ) } } 

iPhone 6S Plus (physical device) iOS 10

enter image description here

iPhone 7 Plus (simulator) iOS 10

enter image description here

enter image description here

Why is the snapshot on the simulator empty?

GitHub Testing Project

+5
source share
1 answer

Add the UIView extension,

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

Update your next code,

 let snapshot = fromVC?.view.snapshotView(afterScreenUpdates: false) 

with,

 let snapshot = fromVC?.view.snapshotView() 

Link

+6
source

Source: https://habr.com/ru/post/1258625/


All Articles