iOS 10 - 自定义动画后的黑屏

时间:2016-07-18 08:46:57

标签: ios swift animation view ios10

我有一个正常工作的自定义动画,但在动画结束时会出现黑屏。

过渡的代码是:

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)

        }

    }
}

调用动画的第一个视图控制器的代码:

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
}

2 个答案:

答案 0 :(得分:1)

使用UIViewPropertyAnimator.runningPropertyAnimator代替UIView.animate

答案 1 :(得分:0)

Xcode 8的第一个测试版中有一个错误。它在第二个测试版中得到了解决。