0

新しいviewControllerをプッシュするときにカスタムズームイントランジションを作成しましたが、以前は完全に正常に機能していました。ビューコントローラーをポップするときにズームアウト効果を作成したい のですが、最終状態が正しいと思っても、アニメーションが間違っているのは、プッシュまたはポップであり、すべてのメソッドがisBeingPresented返されるかどうかを識別する方法がわからないためです。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 に答える