如何使用交互式过渡呈现视图控制器

时间:2014-04-09 06:13:45

标签: iphone objective-c ios7 custom-transition

我的要求是当我从屏幕右侧滑动(viewcontroller A)然后需要使用交互式转换推送到下一个viewcontroller(viewcontroller B)。当我从屏幕左侧滑动时,使用相同的机制进行解除( viewcontroller B)它使用交互式转换关闭控制器。如何以正确的方式实现它。我已经实现了使用交互式转换来解除视图控制器但是无法使用交互式转换实现推送到视图控制器

   #import "AMSimpleAnimatedDismissal.h"

@implementation AMSimpleAnimatedDismissal
-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    return 1.5f;
}

-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    [[transitionContext containerView]addSubview:toVC.view];
    CGRect toFrame = toVC.view.frame;
    CGRect initialFrame = fromVC.view.frame;
    toVC.view.frame = CGRectMake(-320 ,0, CGRectGetWidth(toFrame) , CGRectGetHeight(toFrame));

    CGRect finalFrame = CGRectMake(initialFrame.size.width, initialFrame.origin.y, initialFrame.size.width, initialFrame.size.height);
    UIViewAnimationOptions opts = UIViewAnimationOptionCurveLinear;
    [UIView animateWithDuration:1.0 delay:0 options:opts animations:^{
        fromVC.view.frame = finalFrame;
        toVC.view.frame = CGRectMake(0, 0, CGRectGetWidth(fromVC.view.frame), CGRectGetHeight(fromVC.view.frame));
    } completion:^(BOOL finished) {
       [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];

}

这些是我的交互式转换部分代码

2 个答案:

答案 0 :(得分:2)

可以设置滑动手势的方向:当调用动作方法时,只需检查滑动方向,如果方向是UISwipeGestureRecognizerDirectionRight,则使用[self.navigationController popViewControllerAnimated:BOOL] 。您可能需要添加&#34; isPop&#34; BOOL属性为AMSimpleAnimatedDismissal并让此类处理演示和解雇。

答案 1 :(得分:0)

如果使用导航控制器进行交互式自定义转换,则必须:

  • delegate

  • 指定UINavigationController
  • 您的UINavigationControllerDelegate必须在以下委托协议方法中指定动画控制器:

    - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                      animationControllerForOperation:(UINavigationControllerOperation)operation
                                                   fromViewController:(UIViewController *)fromVC
                                                     toViewController:(UIViewController *)toVC
    
  • 如果您希望推送是交互式的,您还必须使用以下方法指定交互控制器:

    - (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController
                             interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController
    

    如果手势发起,此方法通常应返回UIPercentDrivenInteractiveTransition,否则返回nil

  • 您必须在视图上添加手势识别器(例如,UISwipeFromEdgeGestureRecognizer),这将启动转换并更新上述UIPercentDrivenInteractiveTransition方法返回的interactionControllerForAnimationController,完成手势后完成或取消转换。