如何在将 xib 文件添加到视图控制器时从右侧进行动画处理



在这里,我在点击导航栏中的菜单按钮时从xib添加一个视图,我的问题是这个视图是从左到右,但我希望它来自右到左。

UIView.animateWithDuration(0.3, animations: { () -> Void in    
navController.navigationBar.frame = CGRectMake(-nibView.frame.width, 20, 
navController.navigationBar.frame.width, 
navController.navigationBar.frame.height)         
nibView.frame = CGRectMake( viewSize.frame.width - nibView.frame.width , 0, 
viewSize.frame.width, viewSize.frame.height)                      
viewSize.addSubview(nibView)
})

er..第一。

创建一个名为 Animator 的新类并继承 NSObject 和 UIViewControllerAnimatedTransitioning。

然后将属性添加到类中:

let animationDuration = 2.0
var operation: UINavigationControllerOperation = .Push

然后实现两个函数:

func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
     return animationDuration 
}
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
}

然后在 mainViewController 中,扩展 UINavigationCOntrollerDelegate,在 viewDidLoad() 中实现 delegate = self 并声明:

let transition = Animator()

在扩展中,添加此函数:

func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
   transition.operation = operation
   return transition 
}

在 Animator 中,实现您的 UIViewControllerContextTransitioning。

weak var storedContext: UIViewControllerContextTransitioning?

然后在函数动画转换中:

storedContext = transitionContext
let fromVC = transitionContext.viewControllerForKey( UITransitionContextFromViewControllerKey) as! MasterViewController
let toVC = transitionContext.viewControllerForKey( UITransitionContextToViewControllerKey) as! DetailViewController
transitionContext.containerView()?.addSubview(toVC.view)

最后,创建自己的动画师。 :)

最新更新