定制controller转场动画

定制controller转场动画

 

从iOS7开始就可以自由定制控制器间的转场动画了,以下实例描述最简单的定制方式,达到的效果如下所示:

为了实现这个效果需要这么多的文件-_-!!!!

RootViewController

//
//  RootViewController.h
//  ControllerCustom
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end

RootViewController.h
//
//  RootViewController.m
//  ControllerCustom
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"

#import "PresentingAnimator.h"
#import "DismissingAnimator.h"

#import "ModelViewController.h"

@interface RootViewController ()<UIViewControllerTransitioningDelegate>

@property (nonatomic, strong) UIButton *button;

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor cyanColor];

    _button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    _button.backgroundColor = [UIColor blackColor];
    _button.layer.cornerRadius = 5;
    [_button setTitle:@"present"
             forState:UIControlStateNormal];
    _button.center = self.view.center;
    [self.view addSubview:_button];

    [_button addTarget:self
                action:@selector(buttonEvent:)
      forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonEvent:(id)sender
{
    // 推出控制器
    ModelViewController *modalViewController = [ModelViewController new];

    // 设置转场动画代理
    modalViewController.transitioningDelegate = self;

    // 定制转场动画
    modalViewController.modalPresentationStyle = UIModalPresentationCustom;

    [self presentViewController:modalViewController
                       animated:YES
                     completion:NULL];
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
                                                                  presentingController:(UIViewController *)presenting
                                                                      sourceController:(UIViewController *)source
{
    // 推出控制器的动画
    return [PresentingAnimator new];
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    // 退出控制器动画
    return [DismissingAnimator new];
}

@end

RootViewController.m

ModelViewController

//
//  ModelViewController.h
//  ControllerCustom
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ModelViewController : UIViewController

@end

ModelViewController.h
//
//  ModelViewController.m
//  ControllerCustom
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "ModelViewController.h"

@interface ModelViewController ()

@property (nonatomic, strong) UIButton *button;

@end

@implementation ModelViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blueColor];

    _button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    _button.backgroundColor = [UIColor blackColor];
    _button.layer.cornerRadius = 5;
    [_button setTitle:@"dismiss"
             forState:UIControlStateNormal];
    _button.center = self.view.center;
    [self.view addSubview:_button];

    [_button addTarget:self
                action:@selector(buttonEvent:)
      forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonEvent:(id)sender
{
    [self dismissViewControllerAnimated:YES
                             completion:^{

                             }];
}

@end

ModelViewController.m

PresentingAnimator

//
//  PresentingAnimator.h
//  ControllerCustom
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface PresentingAnimator : NSObject<UIViewControllerAnimatedTransitioning>

@end

PresentingAnimator.h
//
//  PresentingAnimator.m
//  ControllerCustom
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "PresentingAnimator.h"

@implementation PresentingAnimator

// 转场动画时间
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
    return 0.5f;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
    // 自己的view
    UIView *fromView = \
        [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view;

    // 另一个view
    UIView *toView   = \
        [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view;

    // 管理容器
    UIView *container = [transitionContext containerView];
    container.backgroundColor = [UIColor blackColor];

    // 容器中添加推出的view
    [container addSubview:fromView];
    [container addSubview:toView];

    // 开始动画(移出fromView,移进toView)
    [UIView animateWithDuration:[self transitionDuration:transitionContext]
                     animations:^{
                         fromView.frame = CGRectMake(10, 10, 320-20, 568-20);

                         // 设置toView从右侧偏移进来
                         CGRect toFrame     = toView.frame;
                         toFrame.origin.x   = container.bounds.size.width; // 偏移一个控制器
                         toView.frame = toFrame;
                         toView.center = container.center;

                     } completion:^(BOOL finished) {
                         // 动画结束
                         [transitionContext completeTransition:YES];
                     }];
}

@end

 PresentingAnimator.m

DismissingAnimator

//
//  DismissingAnimator.h
//  Popping
//
//  Created by André Schneider on 16.05.14.
//  Copyright (c) 2014 André Schneider. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface DismissingAnimator : NSObject <UIViewControllerAnimatedTransitioning>

@end

DismissingAnimator.h
//
//  DismissingAnimator.m
//  Popping
//
//  Created by André Schneider on 16.05.14.
//  Copyright (c) 2014 André Schneider. All rights reserved.
//

#import "DismissingAnimator.h"

@implementation DismissingAnimator

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

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
    // 自己的view
    UIView *fromView = \
    [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view;

    // 另一个view
    UIView *toView   = \
    [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view;
    toView.frame = CGRectMake(10, 10, 320-20, 568-20);

    // 管理容器
    UIView *container = [transitionContext containerView];

    // 容器中添加推出的view
    [container addSubview:toView];
    [container addSubview:fromView];

    container.backgroundColor = [UIColor blackColor];

    // 开始动画(移出fromView,移进toView)
    [UIView animateWithDuration:[self transitionDuration:transitionContext]
                     animations:^{
                         CGRect fromFrame = fromView.frame;
                         fromFrame.origin.x = container.bounds.size.width;
                         fromView.frame = fromFrame;

                         toView.frame = container.frame;

                     } completion:^(BOOL finished) {
                         // 动画结束
                         [transitionContext completeTransition:YES];
                     }];
}

@end

DismissingAnimator.m

核心的地方:

为什么设计成代理呢?其实,这是为了让基本的控制器(推出其他控制器的控制器)持有被推出的控制器而已,我是这么理解的.

 

为了能够实现控制器间的转场动画,我们需要一个实现了UIViewControllerAnimatedTransitioning协议的对象才行.

也就是PresentingAnimator以及DismissingAnimator

最少实现里面的两个方法:

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

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

完了,就是这么简单呢.

 

附录:

这个方法非常关键哦,动画执行完了之后记得设置好了.

 

fromView本身就被transitionContext包含拥有了,你无须进行上面的那个addSubview操作哦,可以直接去掉即可

时间: 2024-08-01 11:48:24

定制controller转场动画的相关文章

定制转场动画ControllerTransitionAnimation

定制转场动画ControllerTransitionAnimation   说明 控制器转场动画的实现晦涩难懂,本人仅在这里实现了非实时(不支持边缘拖拽手势)的转场动画效果,支持实时转换的转场动画还在研究当中.   效果   源码 https://github.com/YouXianMing/ControllerTransitionAnimation // // VirtualAnimator.h // Transition // // Created by YouXianMing on 15/

iOS自定义转场动画实战讲解

转场动画这事,说简单也简单,可以通过presentViewController:animated:completion:和dismissViewControllerAnimated:completion:这一组函数以模态视图的方式展现.隐藏视图.如果用到了navigationController,还可以调用pushViewController:animated:和popViewController这一组函数将新的视图控制器压栈.弹栈. 下图中所有转场动画都是自定义的动画,这些效果如果不用自定义动

iOS 7 present/dismiss自定义转场动画

前言 iOS 7以后提供了自定义转场动画的功能,我们可以通过遵守协议完成自定义转场动画.本篇文章讲解如何实现自定义present.dismiss自定义动画. 关于自定义push/pop转场动画,请阅读iOS 7 push/pop自定义转场动画 效果图 本篇文章实现的动画切换效果图如下: 视图切换种类 如下效果图,这是有两大类视图切换动画的,一种是交互式的,另一种就是自定义的. 本篇只讲其中的UIViewControllerAnimatedTransitioning协议,来实现present.di

ios7 push/pop转场动画

前言 iOS 7之后,苹果提供了自定义转场动画的API,我们可以自己去定义任意动画效果.本篇为笔者学习push.pop自定义转场效果的笔记,如何有任何不正确或者有指导意见的,请在评论中留下您的宝贵意见!!! 请注意:如果要求支持iOS 7以下版本,则不可使用此效果. 我们本篇文章目标效果: 视图切换种类 如下效果图,这是有两大类视图切换动画的,一种是交互式的,另一种就是自定义的. 本篇只讲其中的UIViewControllerAnimatedTransitioning协议,来实现push.pop

深入学习iOS7自定义导航转场动画_IOS

在iOS7以前,开发者如果希望定制导航控制器推入推出视图时的转场动画,一般都只能通过子类化UINavigationController或者自己编写动画代码去覆盖相应的方法,现在iOS7为开发者带来了福音,苹果公司引入了大量新API,给予了开发者很高的自由度,在处理由UIViewController管理的UIView动画时,这些API使用方便,可扩展性也很强,定制起来非常轻松:  全新的针对UIView的动画block方法 全新的UIViewControllerAnimatedTransition

IOS实战之自定义转场动画详解_IOS

转场动画这事,说简单也简单,可以通过presentViewController:animated:completion:和dismissViewControllerAnimated:completion:这一组函数以模态视图的方式展现.隐藏视图.如果用到了navigationController,还可以调用pushViewController:animated:和popViewController这一组函数将新的视图控制器压栈.弹栈. 下图中所有转场动画都是自定义的动画,这些效果如果不用自定义动

教你使用转场动画提升用户体验

  的确,相比于绘制插画.创作专题页面.设计UI这些富有创造性的设计活动而言,设计表单样式似乎确实是一件单调而苦逼的差事.但仔细想想,如果你设计的表单是支付流程中最重要的环节,那么情况就又不一样了,因为它将是用户选择你和你的企业的起点,也是用户信任的明证.所以这个环节的用户体验必须精雕细琢,尽量臻于完美.除了设计好表格和UI样式之外,合理地运用转场动画会让整个体验提高许多. 这个地方所用的动画并不是单纯的愉悦用户,更重要的目的是让用户明白这个环节是会发生什么,并且如何高效地使用这一产品.如果这个

实例讲解iOS中的CATransition转场动画使用_IOS

一.简介CATransition是CAAnimation的子类,用于做转场动画 能够为图层提供移出屏幕和移入屏幕的动画效果.iOS比Mac OS X的转场动画效果少一点 如:UINavigationController导航控制器就是通过CATransition转场动画实现了将控制器的视图推入屏幕的动画效果 CATransition头文件 动画属性: type:动画过渡类型 subtype:动画过渡方向 startProgress:动画起点(在整体动画的百分比) endProgress:动画终点(

[译]Workcation App – 第四部分. 场景(Scenes)和 RecyclerView 的共享元素转场动画(Shared Element Transition)

本文讲的是[译]Workcation App – 第四部分. 场景(Scenes)和 RecyclerView 的共享元素转场动画(Shared Element Transition), 原文地址:Workcation App – Part 4. Shared Element Transition with RecyclerView and Scenes 原文作者:Mariusz Brona 译文出自:掘金翻译计划 译者:龙骑将杨影枫 校对者:张拭心.Feximin Workcation App