2015-10-22 2 views
1

Это мой обычай transitioningDelegate:IOS: Пользовательские 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, чтобы сделать это изменение ясным. Но это не работает. В чем проблема?

ответ

7

Свойство transitioningDelegate слабое, и вы не создаете никаких других сильных ссылок на него. Для чего-то еще нужно владеть этим объектом, чтобы он придерживался достаточно долго, чтобы использовать его для оживления перехода.

+0

Хотелось бы, чтобы я мог его отсрочить десять раз. – Nick

Смежные вопросы