这是我的自定义transitioningDelegate:

enum CameraState {
    case On
    case Off
}

class CameraTransitioning: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {
    var state: CameraState

    init(state: CameraState) {
        self.state = state
    }

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        let containerView = transitionContext.containerView()
        let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)

        var toViewInitialFrame = transitionContext.initialFrameForViewController(toVC!)
        var fromViewFinalFrame = transitionContext.finalFrameForViewController(fromVC!)
        switch self.state {
        case .On:
            toViewInitialFrame.origin.y = containerView!.frame.height
        case .Off:
            fromViewFinalFrame.origin.y = -containerView!.frame.height
        }

        containerView?.addSubview(toView!)
        toView?.frame = toViewInitialFrame

        let duration = self.transitionDuration(transitionContext)
        UIView.animateWithDuration(duration, animations: {
            fromView?.frame = fromViewFinalFrame
            }) {
                finished in
                transitionContext.completeTransition(true)
        }
    }
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 10
    }

    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return self
    }
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return self
    }
}

这就是我的用法:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "Home -> Camera" {
        let cameraVC = segue.destinationViewController as! CameraViewController
        cameraVC.delegate = self
        cameraVC.transitioningDelegate = CameraTransitioning(state: .On)
    }
}

如您所见,我使用此过渡是因为我不喜欢默认的UIViewAnimationCurveEaseInOut,并且我尝试将持续时间设置为10以使此更改清晰可见。但这是行不通的。问题出在哪里?

最佳答案

transitioningDelegate属性是弱的,并且您没有为其创建其他强引用。其他东西需要拥有该对象才能使其停留足够长的时间,以用于动画过渡。

09-07 11:20