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

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


Untitled.gif

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

#import <Foundation/Foundation.h>

@interface PresentTransitionAnimated : NSObject

@end

PresentViewController.h文件


#import "PresentTransitionAnimated.h"
#import <UIKit/UIKit.h>
@interface PresentTransitionAnimated ()<UIViewControllerAnimatedTransitioning>

@end

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

/** 定义动画效果 */
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)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<UITouch *> *)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<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    return [[PresentTransitionAnimated alloc] init];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述 这篇文章,我将讲述几种转场动画的自定义方式,并且每种方式附上一个示例,毕竟代码才是我们的语言,这样比较容易上...
    伯恩的遗产阅读 54,067评论 37 381
  • OC开发我们主要有以下三种自定义方法,供大家参考:Push & PopModalSegue 前两种大家都很熟悉,第...
    ScaryMonsterLyn阅读 1,699评论 1 3
  • 前言 正如标题所示,iOS开发中, 自定义转场的过渡动画确实是必须要了解的, 在iOS7之后实现也是很简单的. 如...
    ZeroJ阅读 9,005评论 9 122
  • 爱是一念之差,最幸福的不过是你曾温柔呼唤,而我恰好有过回应。
    而而阅读 231评论 0 1
  • 小朵备孕时间久 为要儿子计划走 三胎生了七仙女 宝爸浑身直发抖 注:小朵生第一胎是女儿,第二胎是双胞胎女儿,第三胎...
    旖旎i阅读 462评论 3 7