自定义 present自定义转场动画,实现和push一样的效果

今天在github上看了一个自定义present动画的demo,后自己尝试做了一个类似push的动画
自定义PresentViewController实现与push一样的动画,想要实现其他的动画,基本思想差不多


自定义 present自定义转场动画,实现和push一样的效果_第1张图片
Untitled.gif

1、首先实现PresentViewController动画,新建一个类继承自NSObject,并遵守UIViewControllerAnimatedTransitioning协议
PresentViewController.m文件

#import 

@interface PresentTransitionAnimated : NSObject

@end

PresentViewController.h文件


#import "PresentTransitionAnimated.h"
#import 
@interface PresentTransitionAnimated ()

@end

@implementation PresentTransitionAnimated
/** 定义动画时间 */
- (NSTimeInterval)transitionDuration:(id)transitionContext{
    return 1.0;
}

/** 定义动画效果 */
- (void)animateTransition:(id)transitionContext{
    
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    //转场的容器图,动画完成之后会消失
    UIView *containerView =  [transitionContext containerView];
    UIView *fromView = nil;
    UIView *toView = nil;
    
    if ([transitionContext respondsToSelector:@selector(viewForKey:)]) {
        fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
        toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    }else{
        fromView = fromViewController.view;
        toView = toViewController.view;
    }
    
    //对应关系
    BOOL isPresent = (toViewController.presentingViewController == fromViewController);
    
    CGRect fromFrame = [transitionContext initialFrameForViewController:fromViewController];
    CGRect toFrame = [transitionContext finalFrameForViewController:toViewController];
    
    if (isPresent) {
        fromView.frame = fromFrame;
        toView.frame = CGRectOffset(toFrame, toFrame.size.width, 0);
        [containerView addSubview:toView];
    }
    
    NSTimeInterval transitionDuration = [self transitionDuration:transitionContext];
    
    [UIView animateWithDuration:transitionDuration animations:^{
        if (isPresent) {
            toView.frame = toFrame;
            fromView.frame = CGRectOffset(fromFrame, fromFrame.size.width*0.3*-1, 0);
        }
    } completion:^(BOOL finished) {
        BOOL isCancelled = [transitionContext transitionWasCancelled];
        
        if (isCancelled)
            [toView removeFromSuperview];
        
        [transitionContext completeTransition:!isCancelled];
    }];   
}

2、新建控制器ViewController, 并遵守UIViewControllerTransitioningDelegate

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UIViewController *presentVC = [[UIViewController alloc]init];
    presentVC.transitioningDelegate = self;
    presentVC.view.backgroundColor = [UIColor redColor];
    [self presentViewController:presentVC animated:YES completion:nil];
}
#pragma Mark - UIViewControllerTransitioningDelegate
- (id)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    return [[PresentTransitionAnimated alloc] init];
}

你可能感兴趣的:(自定义 present自定义转场动画,实现和push一样的效果)