我将如何开始复制Apple在iOS 7中默认使用的自定义视图控制器转换?

使用iOS 7的自定义视图控制器转换,我想实现类似于iOS 7中的苹果默认视图控制器转换的视觉效果。

(您可以滑动的位置,通过从左向右滑动视图控制器来popup视图控制器,其中顶部视图控制器用阴影滑动另一个顶部视图控制器,并且导航栏移动。)

在这里输入图像说明

不过,我在实施这个过程中遇到了很多困难。 自定义视图控制器上的大多数教程与默认情况下有非常不同的效果,以显示API的function,但我想复制这一个。

在我的子类中实现<UIViewControllerAnimatedTransitioning>我有以下代码的交互式animation:

 - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext { UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; [transitionContext.containerView addSubview:toViewController.view]; [transitionContext.containerView addSubview:fromViewController.view]; fromViewController.view.layer.shadowOffset = CGSizeMake(0.0, 0.0); fromViewController.view.layer.shadowColor = [UIColor blackColor].CGColor; fromViewController.view.layer.shadowRadius = 5.0; fromViewController.view.layer.shadowOpacity = 0.5; [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{ CGRect newFrame = fromViewController.view.frame; newFrame.origin.x = CGRectGetWidth(fromViewController.view.bounds); fromViewController.view.frame = newFrame; } completion:^(BOOL finished) { [transitionContext completeTransition:!transitionContext.transitionWasCancelled]; }]; } 

然而,阴影代码使其滞后(即使我使用新的snapshot方法),我不知道如何操纵导航栏

有没有人试图做类似的事情,并能够提供示例代码?

示例项目进行testing,如果你想: http : //cl.ly/0B3q1b390x0D

感谢objc.io的基本代码。

设置shadowPath大大提高了这个阴影的性能。

在设置了阴影属性之后,只需在animateTransition:方法中添加此项即可。 这避免了影子通常造成的昂贵的离屏渲染。

 [fromViewController.view.layer setShadowPath:[[UIBezierPath bezierPathWithRect:fromViewController.view.bounds] CGPath]]; 

我下载了你的示例项目,做了这个,口吃现在完全消失了。

一些信息在这里做了什么。

编辑:

关于操纵导航栏animation的答案是,它似乎不可能。 为了做到这一点,你需要从头开始重新实现你自己的NavigationControllertypes的类。 导航栏的转换animation由容器视图控制器( UINavigationController )内部完成,并且不在代码的任何位置浮出水面。

看看ECSlidingViewController我已经使用了它几次,如果你明白它是如何工作的,你可以轻松地实现类似的东西。

如果你想重新发明轮子,我的答案是没有用的,对不起。

我使用dynamic创build效果我用下面的方法创build我自己的ViewController子类。

 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil forMultipleDirections:(BOOL)isTwoDirections; - (void)addVelocityForGesture:(UIPanGestureRecognizer *)recognizer; - (UIDynamicItemBehavior*) itemBehaviourForView; 

然后,我添加了物理和一个手势识别器,以处理paning手势。 使其看起来只是添加阴影偏移量。

 _animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.parentViewController.view]; _gravity = [[UIGravityBehavior alloc] init]; _gravity.gravityDirection = CGVectorMake(-1.5, 0); [_animator addBehavior:_gravity]; UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.view]]; [collision addBoundaryWithIdentifier:@1 fromPoint:CGPointMake(0, 0) toPoint:CGPointMake(0, self.parentViewController.view.frame.size.height)]; [_animator addBehavior:collision]; [_gravity addItem:self.view]; UIDynamicItemBehavior *itemBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[self.view]]; [itemBehavior setElasticity:0.2]; [_animator addBehavior:itemBehavior]; 

你将需要更多的方法来增加速度根据平移的方向,那么你将需要find一个点,你想解雇或隐藏childViewController取决于你的用例,等等…我做到了,它的工作很漂亮对我来说很好,因为它使用Dynamics,它只支持iOS7 +