iOS -自定义转场动画

青春.png
*废话不多说直接看效果(模仿新浪微博)
自定义视图弹窗.gif

-分析我们的效果,点击导航的titleView然后弹出一个自定义视图,里面有个tableView。 其次有个浅灰色的蒙版

-工程结构

Snip20170317_1.png
Pragma mark - NO.1 主界面设置
- (void)viewDidLoad {
    [super viewDidLoad];
  [self setupNav];
  }


-(void)setupNav{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"navigationbar_friendattention"] style: UIBarButtonItemStylePlain target:self action:nil];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"navigationbar_pop"] style: UIBarButtonItemStylePlain target:self action:nil];
    
    self.titleBtn = [[TitleButton alloc]init];
    [self.titleBtn setTitle:@"tmack81" forState:UIControlStateNormal];
    [self.titleBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.titleBtn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.navigationItem.titleView = self.titleBtn;
    
}


-以上是最简单主界面导航栏的设置,可以忽略

Pragma mark — NO.2 PopoverViewController设置
#首先创建一个PopoverViewController,继承自UIViewController,创建时给个xib文件,在里面整个tableView就好,其次在里面不需要设置任何东西

 在ViewController中,按钮的点击事件
//按钮监听事件
-(void)titleBtnClick:(TitleButton *)titleBtn{
     titleBtn.selected = !titleBtn.selected;
  
    //创建弹出控制器
    PopoverViewController *popover = [[PopoverViewController alloc]init];
    
    //设置modal样式--因为不设置的话会造成popoverVc弹出的时候,首页和nav或者tabBar全部被移除掉了,而我的popoverVc不是遮住整个屏幕的,所以不能让底部的控制器移除掉了
    popover.modalPresentationStyle = UIModalPresentationCustom; //设置成这个就不会移除了
  //弹出控制器
    [self presentViewController:popover animated:YES completion:nil];
    
    
}


Pragma mark — NO.3 关键设置JJPresentViewController
#import <UIKit/UIKit.h>

@interface JJPresentationController : UIPresentationController

/**蒙版View*/
@property (nonatomic , strong) UIView *coverView;
@end
#设置JJPresentationController继承自UIPresentationController,并创建一个蒙版属性

.m文件中进行设置
#import "JJPresentationController.h"

@implementation JJPresentationController

//Modal出来的控制器实际上都是先加到一个容器上的,我们重写这个即将布局容器的子控制器的方法来控制modal出来的控制器的大小
-(void)containerViewWillLayoutSubviews{
    [super containerViewWillLayoutSubviews];
     //1.拿到被弹出的控制器设置其大小
    self.presentedView.frame = CGRectMake(0, 0, 200, 300);
    self.presentedView.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, 55);
    //2.设置蒙版
    [self setupCoverView];
    
    
}

//设置蒙版View
-(void)setupCoverView{
    
    //1.添加蒙版
    //如果用addSubview会将我们弹出控制器的view一并遮住了
    //把coverView添加到容器view的最底层
    
    self.coverView = [[UIView alloc]init];
    [self.containerView insertSubview:self.coverView atIndex:0];
    
    //设置coverView的大小
    self.coverView.frame = self.containerView.bounds;
    self.coverView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.3];
    //给coverView添加点击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];
    [self.coverView addGestureRecognizer:tap];
    
    
    
}

#pragma mark -- 点击手势的监听方法
-(void)tapClick:(UITapGestureRecognizer *)tap{
      //直接可以拿到弹出的控制器,然后dismiss就好了
    [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
   // NSLog(@"点击蒙版了");
}



Pragma mark — NO.4 回到主界面继续设置
#1.遵守两个协议
@interface ViewController ()<UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning>

#2.做个属性记录
/**记录是弹出还是消失动画*/
@property (nonatomic , assign) BOOL isPresented;
//默认是消失动画
    self.isPresented = NO;

#3.在按钮点击事件中设置转场代理
popover.transitioningDelegate = self;

#4.执行协议方法并设置弹出和消失动画

#pragma mark -- 代理方法

-(UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source{
    
    JJPresentationController *presentVc = [[JJPresentationController alloc]initWithPresentedViewController:presented presentingViewController:presenting];
    
    return presentVc;
    
}


#pragma mark -- 自定义弹出动画
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    
    self.isPresented = YES;
    //返回值是转场动画协议,我们直接返回就好
    return self;
    
}

#pragma mark -- 自定义消失动画
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    self.isPresented = NO;
    return self;
}

// MARK:- 自定义弹出动画遵守的协议(报错是因为这个协议里面有必须实现的方法)
#pragma mark -- UIViewControllerAnimatedTransitioning 实现这个协议的代理方法
//返回动画时间
-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    return  0.8;
    
}

 ///transitionContext:转场上下文:可以获得弹出的view和消失的view
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    
  //三目运算
 self.isPresented ? [self animationForPresentedView:transitionContext] : [self animationToPresentedView:transitionContext];
 
    
}

#pragma mark -- 自定义弹出动画
-(void)animationForPresentedView:(id<UIViewControllerContextTransitioning>)transitionContext{
    //1.获取弹出的view
    //UITransitionContextFromViewKey:消失的view
    //UITransitionContextToViewKey:弹出的view
    UIView *presentedView = [transitionContext viewForKey:UITransitionContextToViewKey];
    
    //2.将弹出的view添加到containerView中
    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:presentedView];
    
    //3.执行我们自定义的动画--将弹出视图的高度隐藏
    presentedView.transform = CGAffineTransformMakeScale(1.0, 0);
    
    //转场动画的时间,我们直接调用第一个动画执行时间的方法,获得的返回值就是那个时间了
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        
        //恢复原有大小
        presentedView.transform = CGAffineTransformIdentity;
        //设置锚点,不然动画不是从上面开始弹出的
        presentedView.layer.anchorPoint = CGPointMake(0.5, 0);
        
    } completion:^(BOOL finished) {
        //必须告诉转场已经完成了自定义动画
        [transitionContext completeTransition:YES];
        
    }];
    
}

#pragma mark -- 自定义消失动画
-(void)animationToPresentedView:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    //1.获取消失的View
    UIView *dismissView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    
    //2.执行动画
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
       
        //这里不能把y方向缩放,直接置为0,这样的话会直接消失,效果不好
        dismissView.transform = CGAffineTransformMakeScale(1.0, 0.000001);
        
        
        
    } completion:^(BOOL finished) {
        
        //将消失View从父控件中移除
        [dismissView removeFromSuperview];
        //告诉其转场动画结束
        [transitionContext completeTransition:YES];
        
    }];
    
    
    
}

Pragma mark — NO.5 至此已经能实现这个自定义视图转场了,最后我们对转场的代码进行一个抽取,使主控制器显得不是那么臃肿
#创建一个类PopoverAnimator继承自NSObject
#import <UIKit/UIKit.h>

//遵守协议
@interface PopoverAnimator : NSObject <UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning>
/**记录是弹出还是消失动画*/
@property (nonatomic , assign) BOOL isPresented ;


#把主控制器里面关于转场的代码全部放过来
#import "PopoverAnimator.h"
#import "JJPresentationController.h"
@implementation PopoverAnimator

#pragma mark -- 代理方法

-(UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source{
    
    JJPresentationController *presentVc = [[JJPresentationController alloc]initWithPresentedViewController:presented presentingViewController:presenting];
    
    return presentVc;
    
}


#pragma mark -- 自定义弹出动画
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    
    self.isPresented = YES;
     return self;
    
}

#pragma mark -- 自定义消失动画
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    self.isPresented = NO;
    return self;
}


#pragma mark -- UIViewControllerAnimatedTransitioning 实现这个协议的代理方法
-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    return  0.8;
    
}

///transitionContext:转场上下文:可以获得弹出的view和消失的view
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    //三目运算
    self.isPresented ? [self animationForPresentedView:transitionContext] : [self animationToPresentedView:transitionContext];
    
    
}

#pragma mark -- 自定义弹出动画
-(void)animationForPresentedView:(id<UIViewControllerContextTransitioning>)transitionContext{
    //1.获取弹出的view
    //UITransitionContextFromViewKey:消失的view
    //UITransitionContextToViewKey:弹出的view
    UIView *presentedView = [transitionContext viewForKey:UITransitionContextToViewKey];
    
    //2.将弹出的view添加到containerView中
    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:presentedView];
    
    //3.执行我们自定义的动画--将弹出视图的高度隐藏
    presentedView.transform = CGAffineTransformMakeScale(1.0, 0);
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        
        //恢复原有大小
        presentedView.transform = CGAffineTransformIdentity;
        //设置锚点,不然动画不是从上面开始弹出的
        presentedView.layer.anchorPoint = CGPointMake(0.5, 0);
        
    } completion:^(BOOL finished) {
        //必须告诉转场已经完成了自定义动画
        [transitionContext completeTransition:YES];
        
    }];
    
}

#pragma mark -- 自定义消失动画
-(void)animationToPresentedView:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    //1.获取消失的View
    UIView *dismissView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    
    //2.执行动画
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
             dismissView.transform = CGAffineTransformMakeScale(1.0, 0.000001);
       } completion:^(BOOL finished) {
        
        //将消失View从父控件中移除
        [dismissView removeFromSuperview];
        //告诉其转场动画结束
        [transitionContext completeTransition:YES];
        
    }];
    
}

#回到主界面,只需要设置一下代理就好,其它代码就都删掉吧
self.popoverAnimator = [[PopoverAnimator alloc]init];
    popover.transitioningDelegate = self.popoverAnimator;


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

推荐阅读更多精彩内容