自定义推视控制器,带有向下滑动动画

时间:2015-04-07 17:11:56

标签: ios objective-c uiviewcontroller

我想自定义推送和弹出视图控制器使用下拉/向上动画,如下所示:

enter image description here

我尝试更改y位置但它不起作用(它根本不显示动画)。

  [self.navigationController pushViewController:nextController animated:NO];

  self.view.frame = CGRectMake(self.view.frame.origin.x,   self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
  [UIView animateWithDuration:0.6 animations:^{
   self.view.frame = CGRectMake(0, -self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height);
} completion:nil];

有什么建议吗? P / s:在这种情况下我必须使用推视控制器而不是presentViewController

更新
我试着像这样使用UINavigationControllerDelegate:
PropertyViewController.h

@interface PropertyViewController : UIViewController <UINavigationControllerDelegate>{

}

<强烈> PropertyViewController.m 。

 - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
  NSLog(@"willShowViewController");
}
 - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
NSLog(@"didShowViewController");
}

 -(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {

  // THIS METHOD IS NOT CALLED AT ALL
  NSLog(@"animationControllerForOperation");
  TLTransitionAnimator *animator = [TLTransitionAnimator new];
  animator.presenting = (operation == UINavigationControllerOperationPush);
   animator.duration = 0.5;
   return animator;
 }

 - (void)viewGallery{
   // PUSH VIEW CONTROLLER
   GalleryViewController* galleryController = [[GalleryViewController alloc] initWithNibName:@"GalleryViewController" bundle:nil]; 
   galleryController.navigationController.delegate = self; 
   [self.navigationController pushViewController:galleryController animated:YES];
 }

更新2
修复了 PropertyViewController.m

中未使用此行调用的问题方法
  self.navigationController.delegate = self;

我面对另一个问题 推送时的向下滑动动画确实有效,但是向上滑动则没有。
这是我的自定义动画:

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];

[[transitionContext containerView] addSubview:toViewController.view];
if (self.presenting) { // push
    fromViewController.view.transform = CGAffineTransformIdentity;
    toViewController.view.transform = CGAffineTransformMakeTranslation(0, toViewController.view.frame.size.height);
}else{ // pop
    fromViewController.view.transform = CGAffineTransformMakeTranslation(0, fromViewController.view.frame.size.height);
}

[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
    if (self.presenting) { // push
        fromViewController.view.transform = CGAffineTransformMakeTranslation(0, toViewController.view.frame.size.height);
        toViewController.view.transform = CGAffineTransformMakeTranslation(0, toViewController.view.frame.size.height);
    } else { // pop
        fromViewController.view.transform = CGAffineTransformMakeTranslation(0, 0);
    }
} completion:^(BOOL finished) {
    toViewController.view.transform = CGAffineTransformIdentity;

    [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}];
}

问题2
    下拉动画仅在第一次起作用,从第二次起,布局呈现错误,我不知道为什么会发生这种情况。

我在哪里错了?

2 个答案:

答案 0 :(得分:4)

您应该实施UIViewControllerAnimatedTransitioning

  1. FromViewController.m符合UINavigationControllerDelegate。其他示例代码告诉您符合UIViewControllerTransitioningDelegate,但仅当您呈现 ToViewController时才会这样。

    @interface ViewController : UIViewController <UINavigationControllerDelegate>
    
  2. FromViewController中的委托回调方法中返回自定义转换动画对象:

    - (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                   animationControllerForOperation:(UINavigationControllerOperation)operation
                                                fromViewController:(UIViewController *)fromVC
                                                  toViewController:(UIViewController *)toVC {
        TransitionAnimator *animator = [TransitionAnimator new];
        animator.presenting = (operation == UINavigationControllerOperationPush);
        return animator;
    }
    
  3. 创建自定义TransitionAnimator动画师类并粘贴以下示例方法:

    - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext {
        return 0.5f;
    }
    
    - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
    {
        UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
        UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
        [[transitionContext containerView] addSubview:toViewController.view];
    
        if (self.presenting) {
            toViewController.view.transform = CGAffineTransformMakeTranslation(0, toViewController.view.frame.size.height);
        }
    
        [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
            if (self.presenting) {
                toViewController.view.transform = CGAffineTransformIdentity;
            } else {
                fromViewController.view.transform = CGAffineTransformMakeTranslation(0, toViewController.view.frame.size.height);
            }
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        }];
    
    }
    
  4. 大部分实施都来自这里,我刚刚根据您的需求编辑了过渡方法:https://stackoverflow.com/a/25026102/2242359

    我仍然强烈建议您阅读部分或全部这些指南:
    http://www.teehanlax.com/blog/custom-uiviewcontroller-transitions/
    http://www.objc.io/issue-5/view-controller-transitions.html
    http://objectivetoast.com/2014/03/17/custom-transitions-on-ios/

答案 1 :(得分:2)

试试这个:

UIViewController *yourViewController = [[UIViewController alloc]init];

[UIView  beginAnimations: @"Showinfo"context: nil];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.75];
[self.navigationController pushViewController: yourViewController animated:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO];
[UIView commitAnimations];