iOS转场动画

转场动画分为两种

导航栏转场
使用模态转场

转场动画就是一个控制器切换到另一个控制器之间的过程,在这个过程中获取到两个控制器,然后对对应的view做出动画

实现转场动画,在脑海中先想一下如果需要实现这样的动画我们都需要哪些东西,首先我们要有一个导航栏控制器,导航栏中有A这个控制器,现在要从A跳到B, A push B的时候是一个动画,B pop A的时候又是一个动画,用一个导航栏model控制展示我们要使用哪种动画。所以我们需要的东西有
一个导航栏控制器
A控制器 B控制器
pushModel ~ A push B的时候需要的动画
popModel ~ B pop A的时候需要的动画
导航栏代理navModel ~ 选择使用动画

  • 我们自定义一个导航栏代理navModel,并遵循实现<UINavigationControllerDelegate>的代理方法,pop动画需要遵循并实现<UIViewControllerInteractiveTransitioning>代理方法
  • 将要跳转的控制器导航栏代理设置为我们的导航栏代理navModel
    self.navigationController.delegate = self.navModel;
  • 创建转场动画需要的pushModel和popModel,遵循并实现<UIViewControllerAnimatedTransitioning>方法。所需要的动画也是在代理方法中实现
  • 在navModel中设置对应的转场所需要的model

导航栏转场

1.设置导航栏代理model,遵守并实现<UINavigationControllerDelegate>协议,下面的方法是操作行为是push还是pop
- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                            animationControllerForOperation:(UINavigationControllerOperation)operation
                                                         fromViewController:(UIViewController *)fromVC
                                                           toViewController:(UIViewController *)toVC{
    if (operation == UINavigationControllerOperationPush) {
        return self.customPush;
        
    }else if (operation == UINavigationControllerOperationPop){
        return self.customPop;
    }
    return nil;
}
2.遵守并实现<UIViewControllerAnimatedTransitioning>协议,该协议中有两个方法都必须实现,一个是动画时间,另一个是具体的转场效果实现,这里并没有设置两个动画model,是否选择用两个根据情况来决定
//转场动画时间
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{
    return 0.5;
}

//转场动画效果
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
    //转场过渡的容器view  -- UINavigationTransitionView
    UIView *containerView = [transitionContext containerView];
    
    //FromVC
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIView *fromView = fromViewController.view;
    fromView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    
    //ToVC
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *toView = toViewController.view;
    toView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    
    //此处判断是push,还是pop 操作
    BOOL isPush = ([toViewController.navigationController.viewControllers indexOfObject:toViewController] > [fromViewController.navigationController.viewControllers indexOfObject:fromViewController]);
    
    if (isPush) {
        [containerView addSubview:fromView];
        [containerView addSubview:toView];//push,这里的toView 相当于secondVC的view
        toView.frame = CGRectMake(kScreenWidth, kScreenHeight, kScreenWidth, kScreenHeight);
    
    }else{
        [containerView addSubview:toView];
        [containerView addSubview:fromView];//pop,这里的fromView 也是相当于secondVC的view
        fromView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    }
    //因为secondVC的view在firstVC的view之上,所以要后添加到containerView中
    
    //动画
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        if (isPush) {
            toView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        }else{
            fromView.frame = CGRectMake(kScreenWidth, kScreenHeight, kScreenWidth, kScreenHeight);
        }
    } completion:^(BOOL finished) {
        BOOL wasCancelled = [transitionContext transitionWasCancelled];
        //设置transitionContext通知系统动画执行完毕
        [transitionContext completeTransition:!wasCancelled];
    }];
}

模态转场动画

1.先在A控制器中设置transitioningDelegate代理为model,model遵循并实现<UIViewControllerTransitioningDelegate>代理方法,
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    // Presented 转场动画model
    return self.customPush;
}

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    //Dismiss转场动画model
    return self.customPop;
}
2.第二步同导航栏转场一样

交互式转场

  • 在B控制器中设置代理方法
  • 在navModel中设置交互手势的类
  • 手势类panModel继承自<UIPercentDrivenInteractiveTransition> 类,在.m文件中实现对应的转场动画
### 设置push或者pop需要用到的类
- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                            animationControllerForOperation:(UINavigationControllerOperation)operation
                                                         fromViewController:(UIViewController *)fromVC
                                                           toViewController:(UIViewController *)toVC{
    if (operation == UINavigationControllerOperationPush) {
        return self.customPush;
        
    }else if (operation == UINavigationControllerOperationPop){
        return self.customPop;
    }
    return nil;
}
###设置交互手势
- (nullable id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController
                                   interactionControllerForAnimationController:(nonnull id<UIViewControllerAnimatedTransitioning>)animationController{
    if (self.gestureRecognizer)
        return self.percentIntractive;
    else
        return nil;
    
}

设置转场交互动画

#import "LYNavKuGouPercentDerivenInteractive.h"

@interface LYNavKuGouPercentDerivenInteractive ()

@property (nonatomic, strong, readonly) UIPanGestureRecognizer *gestureRecognizer;

@end

@implementation LYNavKuGouPercentDerivenInteractive

- (void)startInteractiveTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
    
    //此句话的重要性
    [super startInteractiveTransition:transitionContext];
}

- (instancetype)initWithGestureRecognizer:(UIPanGestureRecognizer *)gestureRecognizer
{
    self = [super init];
    if (self)
    {
        _gestureRecognizer = gestureRecognizer;
        
        [_gestureRecognizer addTarget:self action:@selector(gestureRecognizeDidUpdate:)];
    }
    return self;
}

- (void)dealloc
{
    [self.gestureRecognizer removeTarget:self action:@selector(gestureRecognizeDidUpdate:)];
}

- (CGFloat)percentForGesture:(UIPanGestureRecognizer *)gesture
{
    
    CGPoint translation = [gesture translationInView:gesture.view];
    
    CGFloat scale = 1 - fabs(translation.x / kScreenWidth);
    scale = scale < 0 ? 0 : scale;
    return scale;
}
- (void)gestureRecognizeDidUpdate:(UIPanGestureRecognizer *)gestureRecognizer
{
    CGFloat scale = 1 - [self percentForGesture:gestureRecognizer];
    NSLog(@"interactive %f",scale);
    
    switch (gestureRecognizer.state)
    {
        case UIGestureRecognizerStateBegan:
            //没用
            
            break;
        case UIGestureRecognizerStateChanged:
            
            [self updateInteractiveTransition:scale];
            
            break;
        case UIGestureRecognizerStateEnded:
            
            if (scale < 0.2f){
                
                [self cancelInteractiveTransition];
            }
            else{
                [self finishInteractiveTransition];
            }
            break;
        default:
            [self cancelInteractiveTransition];
            break;
    }
}

注意点

  • 如果是交互性的转场,需要navModel同时遵循并实现<UIViewControllerInteractiveTransitioning>代理方法,在B控制器中设置导航栏代理方为navModel
  • 导航栏转场设置
    self.navigationController.delegate
    模态转场:
    //1. 设置代理
  second.transitioningDelegate = self.animatedTransition;
    //2.跳转
    [self presentViewController:second animated:YES completion:nil];```



参考网址:[仿微信图片浏览转场、酷狗转场 - iOS自定义转场动画(入门篇)](http://www.jianshu.com/p/ec08f43808aa)<这里介绍的很详细>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,158评论 4 370
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,600评论 1 307
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 111,785评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,655评论 0 220
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,075评论 3 295
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,002评论 1 225
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,146评论 2 318
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,918评论 0 211
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,671评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,838评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,318评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,636评论 3 263
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,343评论 3 244
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,187评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,982评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,126评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,934评论 2 279

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,598评论 25 707
  • 一如既往的在看文章之前我们先来看一下Demo的效果: 要实现这样的效果需要哪些技术呢?CAShaperLayer?...
    ty_Chen阅读 6,860评论 3 19
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,644评论 4 59
  • 今天才知道美这个字指的是“有肉吃”。 实际上是,是有羊肉吃! 美,甘也。从羊从大。羊在六畜主給膳也。美与善同意。—...
    张静年阅读 665评论 2 13
  • 问题名:如何在职场中定位并持续成长? 场景描述 : 自身学习的是工程管理,入职的是地方的单位,目前在莫桑比克工作,...
    琳琳93阅读 261评论 3 1