I am trying to do a custom transition animation. I have create an animation object that conforms to UIViewControllerAnimatedTransitioning:
import UIKit
class ViewControllerAnimator: NSObject, UIViewControllerAnimatedTransitioning {
let duration = 1.0
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return duration
}
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
//Animate out
UIView.animateWithDuration(duration, animations: { () -> Void in
fromView.frame.origin.x -= 200
}) { (Bool) -> Void in
transitionContext.completeTransition(true)
}
}
}
I am getting an error when trying to set the frame of fromView. It crashes on trying to force unwrap nil. What am I doing wrong here? Why is my fromView nil?