ControllerAnimatedTransitioning的

ControllerAnimatedTransitioning的

因此,我开始创建自定义的推送/弹出过渡,并且所有工作都在第一个推送过渡中进行,但是当我尝试推送另一个视图控制器时,我根本没有获得任何动画。

Here is a sample project with the code

大量的代码传入:

这是我的UINavigationControllerDelegate类:

@interface CKMNavigationControllerDelegate : NSObject <UINavigationControllerDelegate>

- (instancetype)initWithForwardTransitionObject:(id<UIViewControllerAnimatedTransitioning>)inForwardTransitionObject popTransitionObject:(id<UIViewControllerAnimatedTransitioning>)inBackTransitionObject;

@property(nonatomic, strong) id<UIViewControllerAnimatedTransitioning> forwardTransitionObject;
@property(nonatomic, strong) id<UIViewControllerAnimatedTransitioning> backTransitionObject;

@end

@implementation CKMNavigationControllerDelegate

- (instancetype)initWithForwardTransitionObject:(id<UIViewControllerAnimatedTransitioning>)inForwardTransitionObject popTransitionObject:(id<UIViewControllerAnimatedTransitioning>)inBackTransitionObject
{
    NSAssert( [inForwardTransitionObject conformsToProtocol:@protocol(UIViewControllerAnimatedTransitioning)], @"inForwardTransitionObject does not conform to protocol UIViewControllerAnimatedTransitioning" );
    NSAssert( [inBackTransitionObject conformsToProtocol:@protocol(UIViewControllerAnimatedTransitioning)], @"inBackTransitionObject does not conform to protocol UIViewControllerAnimatedTransitioning" );

    self = [super init];
    if( !self ) return nil;

    _forwardTransitionObject = inForwardTransitionObject;
    _backTransitionObject = inBackTransitionObject;

    return self;
}

- (void)setForwardTransitionObject:(id<UIViewControllerAnimatedTransitioning>)inForwardTransitionObject
{
    NSAssert( [inForwardTransitionObject conformsToProtocol:@protocol(UIViewControllerAnimatedTransitioning)], @"inForwardTransitionObject does not conform to protocol UIViewControllerAnimatedTransitioning" );
    _forwardTransitionObject = inForwardTransitionObject;
}

- (void)setBackTransitionObject:(id<UIViewControllerAnimatedTransitioning>)inBackTransitionObject
{
    NSAssert( [inBackTransitionObject conformsToProtocol:@protocol(UIViewControllerAnimatedTransitioning)], @"inBackTransitionObject does not conform to protocol UIViewControllerAnimatedTransitioning" );
    _backTransitionObject = inBackTransitionObject;
}

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)inNavigationController animationControllerForOperation:(UINavigationControllerOperation)inOperation fromViewController:(UIViewController *)inFroViewController toViewController:(UIViewController *)inToViewController
{
    return  inOperation == UINavigationControllerOperationPush ? self.forwardTransitionObject : self.backTransitionObject;
}

我的类实现了UIViewControllerAnimatedTransitioning的推送转换:
@interface CKMForwardTransitionObject : NSObject <UIViewControllerAnimatedTransitioning>

@end

@implementation CKMForwardTransitionObject

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

    NSAssert([toViewController conformsToProtocol:@protocol(CKMTransitionAnimatable)], @"toViewController does not conform to CKMTransitionAnimatable");
    NSAssert([fromViewController conformsToProtocol:@protocol(CKMTransitionAnimatable)], @"fromViewController does not conform to CKMTransitionAnimatable");

    id <CKMTransitionAnimatable> fromAnimationSource = (id <CKMTransitionAnimatable>)fromViewController;
    id <CKMTransitionAnimatable> toAnimationSource = (id <CKMTransitionAnimatable>)toViewController;

    CGFloat duration = [fromAnimationSource transitionOutDuration] + [toAnimationSource transitionInDuration];

    return duration;
}

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

    NSAssert([toViewController conformsToProtocol:@protocol(CKMTransitionAnimatable)], @"toViewController does not conform to CKMTransitionAnimatable");
    NSAssert([fromViewController conformsToProtocol:@protocol(CKMTransitionAnimatable)], @"fromViewController does not conform to CKMTransitionAnimatable");

    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:toViewController.view];
    [containerView addSubview:fromViewController.view];

    id <CKMTransitionAnimatable> fromAnimationSource = (id <CKMTransitionAnimatable>)fromViewController;
    id <CKMTransitionAnimatable> toAnimationSource = (id <CKMTransitionAnimatable>)toViewController;


    CGFloat animationDelay = [fromAnimationSource transitionOutDuration];
    [fromAnimationSource animateOnTransitionOut:nil];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(animationDelay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [toAnimationSource animateOnTransitionIn:^{
            [transitionContext completeTransition:YES];
        }];
    });

}

ViewController使用自定义过渡的协议将实现:
typedef void (^CKMAnimationCompletionBlock)(void);

@protocol CKMTransitionAnimatable <NSObject>

- (void)animateOnTransitionIn:(CKMAnimationCompletionBlock)inAnimationCompletionBlock;
- (void)animateOnTransitionOut:(CKMAnimationCompletionBlock)inAnimationCompletionBlock;
- (NSTimeInterval)transitionInDuration;
- (NSTimeInterval)transitionOutDuration;

@end

最后,视图控制器中的代码实际上实现了CKMTransitionAnimatable协议:
- (NSTimeInterval)transitionInDuration
{
    return 0.4;
}

- (NSTimeInterval)transitionOutDuration
{
    return 0.4;
}

- (void)animateOnTransitionOut:(CKMAnimationCompletionBlock)inAnimationCompletionBlock
{
    [UIView animateWithDuration:[self transitionOutDuration] animations:^{
        self.contentView.alpha = 0;
    } completion:^(BOOL finished) {
        if( inAnimationCompletionBlock )
        {
            inAnimationCompletionBlock();
        }
    }];
}

- (void)animateOnTransitionIn:(CKMAnimationCompletionBlock)inAnimationCompletionBlock
{
    [UIView animateWithDuration:[self transitionInDuration] animations:^{
        self.contentView.alpha = 1;
    } completion:^(BOOL finished) {
        if( inAnimationCompletionBlock )
        {
            inAnimationCompletionBlock();
        }
    }];
}

一切看起来都很简单,但是对于我一生来说,我看不到自己在做错什么。

最佳答案

找出问题...看起来就像是我在animateTransition中将视图控制器添加到容器视图的方式一样。它看起来应该像这样:

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

    NSAssert([toViewController conformsToProtocol:@protocol(CKMTransitionAnimatable)], @"toViewController does not conform to CKMTransitionAnimatable");
    NSAssert([fromViewController conformsToProtocol:@protocol(CKMTransitionAnimatable)], @"fromViewController does not conform to CKMTransitionAnimatable");

    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:fromViewController.view];


    id <CKMTransitionAnimatable> fromAnimationSource = (id <CKMTransitionAnimatable>)fromViewController;
    id <CKMTransitionAnimatable> toAnimationSource = (id <CKMTransitionAnimatable>)toViewController;

    [fromAnimationSource animateOnTransitionOut:nil];
    CGFloat animationDelay = [fromAnimationSource transitionOutDuration];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(animationDelay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [containerView addSubview:toViewController.view];
        [toAnimationSource animateOnTransitionIn:^{
            [transitionContext completeTransition:YES];
        }];
    });

}

08-04 18:52