提前感谢您的帮助!
所以我跟着这个tutorial,我创建了自己的自定义动画师。
我的CustomAnimator
课已经完成,但我根本就没有得到如何使用它。
我为ViewController
UINavigationControllerDelegate
创建了一个扩展程序,现在我的问题是,如何在ViewController
中使用CustomAnimator
转换为切换到我的按钮NewViewController
?
非常感谢,我喜欢这个社区;)
圣拉斐尔
编辑: 以下是您需要了解的代码: LoginViewController的扩展
extension LoginViewController: UINavigationControllerDelegate {
func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
// Creating an instance of the animator
let animation = TopDownScrollAnimation()
if fromVC is LoginViewController {
animation.transitionMode = .Present
} else {
animation.transitionMode = .Dismiss
}
return animation
}
}
自定义动画:
class TopDownScrollAnimation: NSObject, UIViewControllerAnimatedTransitioning {
enum TransitionMode: Int {
case Present, Dismiss
}
var transitionMode: TransitionMode = .Present
// Transition duration
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
switch transitionMode {
case .Present:
return 0.4
case.Dismiss:
return 0.2
}
}
// Transition animation
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
// Getting containterView + both views for the transition
let containerView = transitionContext.containerView()
let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
switch transitionMode {
case .Present:
// Adding toView to the containerView
toView.frame = CGRect(x: containerView!.bounds.size.width, y: 0, width: containerView!.bounds.size.width, height: containerView!.bounds.size.height)
containerView?.addSubview(toView)
// Adding fromView to the containerView
containerView?.addSubview(fromView)
containerView?.bringSubviewToFront(toView)
// Getting the duration
let duration = self.transitionDuration(transitionContext)
// Animation
UIView.animateWithDuration(duration, animations: {
fromView.frame = CGRectOffset(fromView.frame, -containerView!.bounds.size.width, 0)
toView.frame = containerView!.frame
}) { (_) in
transitionContext.completeTransition(true) }
case .Dismiss:
// Adding the toView to the containerView
containerView?.addSubview(toView)
toView.frame = CGRect(x: -containerView!.bounds.size.width, y: 0, width: containerView!.bounds.size.width, height: containerView!.bounds.size.height)
// Adding the fromView and bringing it to the front
containerView?.addSubview(fromView)
containerView?.bringSubviewToFront(fromView)
// Getting the duration
let duration = self.transitionDuration(transitionContext)
UIView.animateWithDuration(duration, animations: {
fromView.frame = CGRectOffset(fromView.frame, containerView!.bounds.size.width, 0)
toView.frame = containerView!.frame
}) { (_) in
transitionContext.completeTransition(true) }
}
}
}
现在我想从LoginViewController获得一个按钮,该按钮使用动画转换到SignUpViewController,而不使用故事板中的NavigationController。
我还在行viewDidLoad()
LoginViewController
的{{1}}中添加了该内容。