alessionossa
alessionossa

Reputation: 991

iOS 10 - black screen after custom animation

I've a custom animation that works correctly except that, at the end of dismiss animation, there is a black screen.

The code of the transition is:

class FolderAnimationController: NSObject, UIViewControllerAnimatedTransitioning {

    let duration    = 5.0
    var presenting  = true
    var originFrame = CGRect.zero
    var selectedFolderCell: FolderCollectionViewCell?

    func transitionDuration(_ transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return duration
    }

    func animateTransition(_ transitionContext: UIViewControllerContextTransitioning) {

        let containerView = transitionContext.containerView()
        let toViewC = transitionContext.viewController(forKey: UITransitionContextToViewControllerKey)!
        let fromViewC = transitionContext.viewController(forKey: UITransitionContextFromViewControllerKey)!
        let folderViewC = presenting ? fromViewC as! ViewController : transitionContext.viewController(forKey: UITransitionContextToViewControllerKey) as! ViewController
        let projectViewC = presenting ? toViewC as! ProjectViewController : transitionContext.viewController(forKey: UITransitionContextFromViewControllerKey) as! ProjectViewController

        let cellView = presenting ? (folderViewC.folderCollectionView.cellForItem(at: (folderViewC.folderCollectionView.indexPathsForSelectedItems()?.first!)!) as! FolderCollectionViewCell).folderView : projectViewC.containerView
        let cellSnapshot: UIView = presenting ? cellView!.snapshotView(afterScreenUpdates: false)! : cellView!.snapshotView(afterScreenUpdates: false)!
        let cellFrame = containerView.convert(cellView!.frame, from: cellView!.superview)
        cellSnapshot.frame = cellFrame
        cellView!.isHidden = true

        toViewC.view.frame = transitionContext.finalFrame(for: toViewC)
        toViewC.view.layoutIfNeeded()
        toViewC.view.alpha = 0

        presenting ? (projectViewC.containerView.isHidden = true) : (self.selectedFolderCell!.folderView.isHidden = true)
        containerView.addSubview(toViewC.view)
        containerView.addSubview(cellSnapshot)

        UIView.animate(withDuration: duration, animations: {

            toViewC.view.alpha = 1.0

            let finalFrame = self.presenting ? projectViewC.containerView.frame : self.originFrame
            cellSnapshot.frame = finalFrame
        }) { (_) in
            self.presenting ? (projectViewC.containerView.isHidden = false) : (self.selectedFolderCell?.isHidden = false)
            cellSnapshot.removeFromSuperview()
            transitionContext.completeTransition(true)

        }

    }
}

And the code of the first view controller that call the animation:

func animationController(forPresentedController presented: UIViewController, presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    presentAnimator.presenting = true
    presentAnimator.originFrame = openingFrame!
    presentAnimator.selectedFolderCell = selectedCell!

    return presentAnimator
}

func animationController(forDismissedController dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    presentAnimator.presenting = false

    return presentAnimator
}

Upvotes: 4

Views: 1224

Answers (2)

Roman Bambura
Roman Bambura

Reputation: 177

Use UIViewPropertyAnimator.runningPropertyAnimator instead UIView.animate

Upvotes: 1

alessionossa
alessionossa

Reputation: 991

There was a bug in the first beta of Xcode 8. It was resolved on the second beta.

Upvotes: 0

Related Questions