我有一个带有根视图控制器的应用程序和两个额外的视图控制器.出于设计原因,这些应该看起来是根VC的左侧或右侧.这种空间关系应该以它们出现在屏幕上时呈现给用户的方式来表示.也就是说,通过推动动画.
当按下一个新的视图控制器时,它从右边进入屏幕,这样就可以正常工作或者我的两个VC之一.另一方面,我创建了a custom segue based on this example,这将使VC从左到右显示.
这可行,但动画本身并不是无缝的.看起来好像视图从左侧推到根VC上 – 看起来好像在视图出现之前有一个黑色背景可见.
我想知道是否有人会知道如何获得一个完全镜像版本的默认“推”(即从右到左)动画?
解决方法
Check out this article.除了过渡本身之外,它正是你想要的.简而言之:
1)创建一个对象来执行动画
@interface Animator : NSObject <UIViewControllerAnimatedTransitioning> @end
2)成为导航控制器的委托,并在此导航控制器委托方法上回答该动画师的实例:
// in your vc.m @interface ViewController () <UINavigationControllerDelegate> // add this // in view did load self.navigationController.delegate = self; // implement this to pass your animator as the thing to be in charge of the transition animation - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController*)fromVC toViewController:(UIViewController*)toVC { if (operation == UINavigationControllerOperationPush) { return [[Animator alloc] init]; } return nil; }
3)在你的动画师中,回答持续时间:
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext { return 0.25; }
4)所有这些都来自文章.剩下要做的唯一事情是实现从左到右的幻灯片. (我更改了文章的代码,从左到右滑动):
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext { UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; CGRect fromVCFrame = fromViewController.view.frame; CGFloat width = toViewController.view.frame.size.width; [[transitionContext containerView] addSubview:toViewController.view]; toViewController.view.frame = CGRectOffset(fromVCFrame,-width,0); [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{ fromViewController.view.frame = CGRectOffset(fromVCFrame,width,0); toViewController.view.frame = fromVCFrame; } completion:^(BOOL finished) { fromViewController.view.frame = fromVCFrame; [transitionContext completeTransition:![transitionContext transitionWasCancelled]]; }]; }
我在一个小项目中尝试了所有这些,它的工作非常顺利.