0

我在推送一个新的 viewController时创建了一个自定义放大过渡,它过去工作得非常好。现在我想在弹出viewController时创建一个缩小效果 ,即使认为最终状态是正确的,动画也是错误的,因为我不知道如何识别它是推动还是弹出以及所有方法,如isBeingPresentedreturn false 并且presentedViewController总是 nil

-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
  self.transitionContext = transitionContext;

  UIView *containerView =  [transitionContext containerView];
  UIViewController *fromViewController = (UIViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  UIViewController *toViewController = (UIViewController *) [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

  [containerView addSubview:toViewController.view];

  CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  scaleAnimation.duration = [self transitionDuration:transitionContext];
  scaleAnimation.delegate = self;
  scaleAnimation.removedOnCompletion = YES;

  CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
  opacityAnimation.duration = [self transitionDuration:transitionContext];
  opacityAnimation.delegate = self;
  opacityAnimation.removedOnCompletion = YES;

  if (toViewController.isBeingPresented) {
    scaleAnimation.fromValue = [NSNumber numberWithDouble:0];
    scaleAnimation.toValue = [NSNumber numberWithDouble:1];

    opacityAnimation.fromValue = [NSNumber numberWithFloat:1];
    opacityAnimation.toValue = [NSNumber numberWithFloat:0];
  } else {
    scaleAnimation.fromValue = [NSNumber numberWithDouble:1];
    scaleAnimation.toValue = [NSNumber numberWithDouble:0];

    opacityAnimation.fromValue = [NSNumber numberWithFloat:0];
    opacityAnimation.toValue = [NSNumber numberWithFloat:1];
  }

  [toViewController.view.layer addAnimation:scaleAnimation forKey:nil];
  [fromViewController.view.layer addAnimation:opacityAnimation forKey:nil];
}
4

3 回答 3

2

您可以保存一个UINavigationControllerOperation变量:

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

    return self;
}

然后检查它是推送还是弹出:

if (self.navigationOperation == UINavigationControllerOperationPush) {
    // push
} else if (self.navigationOperation == UINavigationControllerOperationPop) {
    // pop
}
于 2015-06-16T16:25:24.603 回答
0

这与您上面的代码无关,但这将覆盖 navigationController.. 的默认动画。

//.h
@interface YJKit_Navigation : UINavigationController

+ (void)navigation:(UINavigationController *)navigationController withViewController:(UIViewController *)viewController push:(BOOL)isPush;

@end

//.m
@implementation YJKit_Navigation

+ (void)navigation:(UINavigationController *)navigationController withViewController:(UIViewController *)viewController push:(BOOL)isPush
{

    [navigationController.view.layer addAnimation: isPush ? PushAnimation : PopAnimation forKey:nil];

    isPush ? [navigationController pushViewController:viewController animated:NO] : [navigationController popViewControllerAnimated:NO];
}

@end

并像这样使用它:

[YJKit_Navigation navigation:YourUINavigationController withViewController:YourTargetUIViewController push:(BOOL)];

希望这也能帮助你.. :)

于 2015-06-16T16:39:57.000 回答
0

查看我的带有弹出动画的 push viewController 变体

- (void)navigationController:(UINavigationController *)navigationController
                 present:(UIViewController *)viewController
                animated:(BOOL)animated
              completion:(void (^)(BOOL finished))completion {
        self.completion = completion;
    if (animated) {
       CATransition *animation = [CATransition animation];
       [animation setDelegate:self];
       [animation setType:kCATransitionMoveIn];
       [animation setSubtype:kCATransitionFromTop];
       [animation setDuration:kPresentAnimationDuration];
       [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

       [navigationController.view.layer addAnimation:animation forKey:kCATransition];
   }

       [navigationController pushViewController:viewController animated:NO];
        if (!animated) {
           self.completion(YES);
       }
}
于 2015-06-16T16:43:04.697 回答