上記の説明からビュー階層とナビゲーション デザインを理解するのは非常に困難です。おそらく、スクリーンショットやスケッチを投稿できますか?
UINavigationController の標準の水平プッシュ/ポップ アニメーションをオーバーライドできます。これを行うには、カスタム UINavigationControllerDelegate オブジェクトとその他いくつかのものを定義します。下記参照。
navController と navControllerDelegate を次のようにセットアップします。
UINavigationController *navigationController = [[UINavigationController alloc] init];
self.navigationControllerDelegate = [[NavigationControllerDelegate alloc] init];
navigationController.delegate = self.navigationControllerDelegate;
NavigationControllerDelegate クラスは次のようになります。
@interface NavigationControllerDelegate : NSObject <UINavigationControllerDelegate>
@end
#import "NavigationControllerDelegate.h"
#import "CrossFadePushAnimator.h"
#import "CrossFadePopAnimator.h"
@interface NavigationControllerDelegate ()
@property (nonatomic, strong) CrossFadePushAnimator* pushAnimator;
@property (nonatomic, strong) CrossFadePopAnimator* popAnimator;
@end
@implementation NavigationControllerDelegate
- (id)init
{
self = [super init];
if (self)
{
self.pushAnimator = [CrossFadePushAnimator new];
self.popAnimator = [CrossFadePopAnimator new];
}
return self;
}
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
if (operation == UINavigationControllerOperationPop)
{
return self.popAnimator;
}
else if (operation == UINavigationControllerOperationPush)
{
return self.pushAnimator;
}
return nil;
}
- (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController
{
return nil;
}
pushAnimator は次のようになります。
@interface CrossFadePushAnimator ()
@property (nonatomic, strong) id<UIViewControllerContextTransitioning> transitionContext;
@end
@implementation CrossFadePushAnimator
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
return AnimationDuration;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
[[transitionContext containerView] addSubview:toViewController.view];
toViewController.view.alpha = 1.0f;
// I'm using CABasicAnimation here for a specific reason, but you could also use the animation method you use above.
CABasicAnimation *fade = [CABasicAnimation animationWithKeyPath:@"opacity"];
fade.fromValue = @0;
fade.toValue = @1;
fade.duration = [self transitionDuration:transitionContext];
fade.removedOnCompletion = YES;
fade.delegate = self;
self.transitionContext = transitionContext;
[toViewController.view.layer addAnimation:fade forKey:AnimationKey];
}
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
[self.transitionContext completeTransition:![self.transitionContext transitionWasCancelled]];
self.transitionContext = nil;
}
popAnimator は pushAnimator に似ています。
お役に立てれば!