oc自定义present,dismiss动画

今天在写项目的广告页,之前是用uiview写的广告页,但是由于没办法在广告页出现的时候隐藏statusBar,无奈想着只能通过控制器写广告页了,因为默认present页面消失的时候是向下退出比较丑,所以打算自定义下退出动画。

刚看了几篇文章关于自定义动画的文章就总结下自定义动画,我这也只是抛砖引玉,写的比较基础,有空再写复杂的切换效果。

既然是自定义控制器切换动画,那么就必须创建一个类并且这个类要继承
UIViewControllerAnimatedTransitioning这个协议
字面意思差不多就是 控制器过渡动画 协议

先上效果图

动画.gif

这是.h文件

#import <UIKit/UIKit.h>
@interface PresentTransition : NSObject<UIViewControllerAnimatedTransitioning>
@end

遵守这个UIViewControllerAnimatedTransitioning协议后主要要实现两个方法:
第一个方法:

- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext;

上面第一个方法主要是返回动画时常的没什么好讲的;

第二个方法

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;

第二个方法就有意思了,注意context这个单词,说明这里包含我们需要做动画的很多东西

通过transitionContext我们可以获取目标控制器

    UIViewController *toVC = (UIViewController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

源控制器

UIViewController *fromVC = (UIViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];

动画时间:

NSTimeInterval duration = [self transitionDuration:transitionContext]

还有最重要的 容器,有了容器view,控制器的view才有地方显示和实现动画

UIView *containerView = [transitionContext containerView];

我这里的present动画是 源控制器从大变小,目标控制器则从右往左覆盖。
所以我这里需要拿到两个控制器的view

UIView *fromView = fromVC.view;
UIView *toView = toVC.view;

//这行代码必须加 不然目标控制器的view都不会出现
[containerView addSubview:toView];

接下来就是实现动画

[UIView animateWithDuration:duration animations:^{
        
        //        fromView.x = containerView.width * (1 - scale) / 2;
        //        fromView.y = containerView.height * (1 - scale) / 2;
        //        fromView.width = containerView.width * scale;
        //        fromView.height = containerView.height * scale;
        
        toView.x = containerView.x;
        fromView.transform = CGAffineTransformMakeScale(0.9, 0.9);
        
    } completion:^(BOOL finished) {
        fromView.transform = CGAffineTransformMakeScale(1, 1);
        [transitionContext completeTransition:YES];
    }];

所以PresentTransition.m文件的代码就是

#import "PresentTransition.h"
@implementation PresentTransition

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    return 0.4;
}

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    return 0.4;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{

    UIViewController *toVC = (UIViewController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController *fromVC = (UIViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    UIView *containerView = [transitionContext containerView];
    NSLog(@"%@", containerView);
    UIView *fromView = fromVC.view;
    UIView *toView = toVC.view;
    
    toView.x = containerView.width;
    
    [containerView addSubview:toView];
    
    NSTimeInterval duration = [self transitionDuration:transitionContext];
    [UIView animateWithDuration:duration animations:^{
        
        //        fromView.x = containerView.width * (1 - scale) / 2;
        //        fromView.y = containerView.height * (1 - scale) / 2;
        //        fromView.width = containerView.width * scale;
        //        fromView.height = containerView.height * scale;
        
        toView.x = containerView.x;
        fromView.transform = CGAffineTransformMakeScale(0.9, 0.9);
        
    } completion:^(BOOL finished) {
        fromView.transform = CGAffineTransformMakeScale(1, 1);
        [transitionContext completeTransition:YES];
    }];
}

自定义动画到这里就结束了,接下来是怎么用:
首先在源控制器.m 遵守协议,.h不变

@interface ViewController ()<UIViewControllerTransitioningDelegate>

然后实现方法 和 代理

- (void)presentBtnClicked{
    
    ZCCSecondViewController *secondVC = [[ZCCSecondViewController alloc] init];
    secondVC.transitioningDelegate = self;//
    [self presentViewController:secondVC animated:YES completion:nil];
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    
    PresentTransition *transition = [[PresentTransition alloc] init];
    transition.transitionType = ZCCTransitionTypePresent;
    return transition;
}

目标控制器操作 .m文件 这里我还写了返回来的动画 所以我目标控制器是这么写的


@interface ZCCSecondViewController ()<UIViewControllerTransitioningDelegate>
@end

- (void)dismissClick{
    
    self.transitioningDelegate = self;
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    PresentTransition *pushAnimation = [[PresentTransition alloc] init];
    pushAnimation.transitionType = ZCCTransitionTypeDismiss;
    return pushAnimation;
}

自定义动画的完整版代码如下

.h文件

#import <UIKit/UIKit.h>

typedef NS_ENUM(NSUInteger, ZCCTransitionType){
    
    ZCCTransitionTypePresent = 0,
    ZCCTransitionTypeDismiss,
    ZCCTransitionTypePush,
    ZCCTransitionTypePop,
};

@interface PresentTransition : NSObject<UIViewControllerAnimatedTransitioning>

@property (nonatomic, assign)ZCCTransitionType transitionType;

@end

.m文件

//
//  PresentTransition.m
//  MOSOBOStudent
//
//  Created by 章程程 on 2018/4/2.
//  Copyright © 2018年 zcc. All rights reserved.
//

#import "PresentTransition.h"
#import "ZCCCommon.h"
@implementation PresentTransition

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    return 0.4;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    switch (_transitionType) {
        case ZCCTransitionTypePresent:
            {
                [self presentAnimation:transitionContext];
            }
            break;
        case ZCCTransitionTypeDismiss:
            {
                [self dismissAnimation:transitionContext];
            }
            break;
        case ZCCTransitionTypePush:
            {
                
            }
            break;
        case ZCCTransitionTypePop:
            {
                
            }
            break;
        default:
            break;
    }
    
}

- (void)presentAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    UIViewController *toVC = (UIViewController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController *fromVC = (UIViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    UIView *containerView = [transitionContext containerView];
    containerView.backgroundColor = [UIColor whiteColor];
    NSLog(@"%@", containerView);
    UIView *fromView = fromVC.view;
    UIView *toView = toVC.view;
    
    toView.x = containerView.width;
    
    [containerView addSubview:toView];
    
    NSTimeInterval duration = [self transitionDuration:transitionContext];
    [UIView animateWithDuration:duration animations:^{
        
        //        fromView.x = containerView.width * (1 - scale) / 2;
        //        fromView.y = containerView.height * (1 - scale) / 2;
        //        fromView.width = containerView.width * scale;
        //        fromView.height = containerView.height * scale;
        
        toView.x = containerView.x;
        fromView.transform = CGAffineTransformMakeScale(0.9, 0.9);
        
    } completion:^(BOOL finished) {
        fromView.transform = CGAffineTransformMakeScale(1, 1);
        [transitionContext completeTransition:YES];
    }];
}

- (void)dismissAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    UIViewController *toVC = (UIViewController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController *fromVC = (UIViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    UIView *containerView = [transitionContext containerView];
    NSLog(@"%@", containerView);
    UIView *fromView = fromVC.view;
    UIView *toView = toVC.view;
    
    [containerView addSubview:toView];
    
    [containerView bringSubviewToFront:fromVC.view];
    
    toView.transform = CGAffineTransformMakeScale(0.9, 0.9);
    
    NSTimeInterval duration = [self transitionDuration:transitionContext];
    
    [UIView animateWithDuration:duration animations:^{
        
        fromView.x = containerView.width;
        toView.transform = CGAffineTransformMakeScale(1.0, 1.0);
        
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
    
}
    
@end
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,752评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,100评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,244评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,099评论 1 286
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,210评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,307评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,346评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,133评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,546评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,849评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,019评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,702评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,331评论 3 319
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,030评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,260评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,871评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,898评论 2 351

推荐阅读更多精彩内容