iOS:自定义transitioningDelegate不起作用

时间:2015-10-22 08:44:43

标签: ios segue uiviewanimationtransition

这是我自定义的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以使此更改变得清晰。但这不起作用。问题出在哪儿?

1 个答案:

答案 0 :(得分:8)

transitioningDelegate属性很弱,并且您没有创建其他强引用。还有其他东西需要拥有该对象才能使其长时间保持足够的状态以用于动画转换。