Answer the question
In order to leave comments, you need to log in
How to display snapshot in NavigationController with custom AnimateTransition?
I need to show a snapshot of a view during an animation, but when I run this code, I get a black (transparent) screen. Snapshot added to containerView, correct frames set, isHidden = false, opaque = true.
In the screen layer debugger, the screen is in the foreground, but black (transparent).
In containerView subviews, it alone is present:
[<_UIReplicantView: 0x7ff1f34138a0; frame = (0 0; 375 667); layer = <_UIReplicantLayer: 0x600000bb57a0>>] Properties: Opaque On Hidden Off
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let fromVC = transitionContext.viewController(forKey: .from),
let fromView = fromVC.view,
let toVC = transitionContext.viewController(forKey: .to),
let toView = toVC.view
else {
return
}
let containerView = transitionContext.containerView
// If remove this, toSnapshot trasparent (but in attributes alpha=1 transparent=0 opaque=false)
fromView.removeFromSuperview()
let toSnapshot = toView.snapshotView(afterScreenUpdates: true)!
containerView.addSubview(toSnapshot)
// Pritingig
print(containerView.subviews)
//transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
Answer the question
In order to leave comments, you need to log in
Something tells me that you absolutely do not understand the logic of transitions between VC and MAIN in what context this happens.
First: What Magic Necessity caused you to delete the controller view you're transitioning from is unclear... But how do you transition back? Going nowhere.
Second: Do you provide your animation class, here Important, not to the Controller you are passing to, but to the Navigation Controller? Something tells me no.
Thirdly: Here is a fully working version. (Worker in the sense that it does what you want. Checks omitted)
.
internal final class ModalAnimator: NSObject, UIViewControllerAnimatedTransitioning {
private var isPresenting: Bool
private let duration: TimeInterval = 3
internal init(isPresenting: Bool) {
self.isPresenting = isPresenting
}
internal func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return duration
}
internal func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let fromVC = transitionContext.viewController(forKey: .from),
let toVC = transitionContext.viewController(forKey: .to),
let snapshot = toVC.view.snapshotView(afterScreenUpdates: true)
else { return }
let containerView = transitionContext.containerView
snapshot.frame = fromVC.view.frame
containerView.addSubview(toVC.view)
containerView.addSubview(snapshot)
fromVC.view.isHidden = true
UIView.animate(
withDuration: duration,
animations: { snapshot.transform = CGAffineTransform(rotationAngle: .pi)},
completion: { _ in
fromVC.view.isHidden = false
snapshot.removeFromSuperview()
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
})
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question