iOS7使用代理自定义导航转场动画

如果你已经厌倦了使用UINavigationController进行简单粗暴的push和pop转场操作,你完全可以使用自定义的导航转场效果,iOS7提供了许多漂亮的代理方法帮助你实现各种自定义动画,下面演示一个简单的导航转场动画Demo的实现过程,效果如图一所示:

STEP 1: 创建一个项目,其中包含,一个名为FirstViewController和一个SecondViewController的视图控制器,两个继承自NSObject的PopAnimation和PushAnimation类。

STEP2:使用UIViewControllerAnimatiedTransitioning代理方法自定义Push 动画

1.)让PopAnimation使用UIViewControllerAnimatedTransitioning代理的方法:

@interface PushAnimation : NSObject <UIViewControllerAnimatedTransitioning>

2.)PopAnimation的实现

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

    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

    [[transitionContext containerView] addSubview:toViewController.view];
    toViewController.view.alpha = 0;

    [UIView animateWithDuration:0.3*[self transitionDuration:transitionContext]  animations:^{
        fromViewController.view.transform = CGAffineTransformMakeScale(0.6, 0.6);
        toViewController.view.alpha = 0.3;
    } completion:^(BOOL finished) {

        [UIView animateWithDuration:0.3*[self transitionDuration:transitionContext]  animations:^{

            fromViewController.view.transform = CGAffineTransformMakeScale(1.1, 1.1);
            toViewController.view.alpha = 0.6;

        } completion:^(BOOL finished){

            [UIView animateWithDuration:0.4*[self transitionDuration:transitionContext] animations:^{

                fromViewController.view.transform = CGAffineTransformMakeScale(0.1, 0.1);
                toViewController.view.alpha = 1.0;
            } completion:^(BOOL finished){

                fromViewController.view.transform = CGAffineTransformIdentity;
                [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
            }];
        }];
    }];
}
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 3.0f;
}

STEP 3: 使用UIViewControllerAnimatiedTransitioning代理方法自定义Pop 动画

@interface PopAnimation : NSObject <UIViewControllerAnimatedTransitioning>
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {

        UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

    [[transitionContext containerView] addSubview:toViewController.view];

    toViewController.view.alpha = 0;
    toViewController.view.transform = CGAffineTransformMakeScale(0.1, 0.1);
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        toViewController.view.transform = CGAffineTransformMakeScale(1.0, 1.0);
        toViewController.view.alpha = 1.0f;
        fromViewController.view.alpha = 0.0f;

    } completion:^(BOOL finished) {
        fromViewController.view.transform = CGAffineTransformIdentity;
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];

    }];
}

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

STEP4: 在FirstViewController中实现UINavigationControllerDelegate的一些方法

#import "PushAnimation.h"
@interface FirstViewController : UIViewController <UINavigationControllerDelegate>
@property (nonatomic, strong) PushAnimation *animation;
- (void)viewDidLoad
{
    [super viewDidLoad];
    _animation = [PushAnimation new];

    //隐藏NavigationBar
    self.navigationController.navigationBarHidden = YES;

    //设置背景图片
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.frame];
    [imageView setImage:[UIImage imageNamed:@"1.jpg"]];
    [self.view addSubview:imageView];

    //定义一个button
    UIButton *pushBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    pushBtn.frame = CGRectMake(284, 700, 200, 40);
    [pushBtn setTitle:@"push" forState:UIControlStateNormal];
    pushBtn.titleLabel.font = [UIFont systemFontOfSize:32.0f];
    [pushBtn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:pushBtn];

}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.navigationController.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.navigationController.delegate == self) {
        self.navigationController.delegate = nil;
    }
}

- (void)buttonClicked:(id)sender {

    SecondViewController *presentController = [[SecondViewController alloc]init];

    [self.navigationController pushViewController:presentController animated:YES];
}

#pragma mask UINavigationControllerDelegate

- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                   animationControllerForOperation:(UINavigationControllerOperation)operation
                                                fromViewController:(UIViewController *)fromVC
                                                  toViewController:(UIViewController *)toVC {
    if (operation == UINavigationControllerOperationPush) {
        return self.animation;
    }
    return nil;
}

STEP5: 在SecondViewController中调用PopAnimation动画

@interface SecondViewController : UIViewController <UINavigationControllerDelegate>
@property(nonatomic, strong) PopAnimation *animation;
- (void)viewDidLoad
{
    [super viewDidLoad];

    _animation = [PopAnimation new];

    //隐藏NavigationBar
    self.navigationController.navigationBarHidden = YES;

    //设置背景图片
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.frame];
    [imageView setImage:[UIImage imageNamed:@"2.jpg"]];
    [self.view addSubview:imageView];

    //定义一个button
    UIButton *pushBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    pushBtn.frame = CGRectMake(284, 700, 200, 40);
    [pushBtn setTitle:@"Pop" forState:UIControlStateNormal];
    pushBtn.titleLabel.font = [UIFont systemFontOfSize:32.0f];
    [pushBtn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:pushBtn];

}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.navigationController.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.navigationController.delegate == self) {
        self.navigationController.delegate = nil;
    }
}

- (void)buttonClicked:(id)sender {

    [self.navigationController popViewControllerAnimated:YES];

}

#pragma mask UINavigationControllerDelegate

- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                   animationControllerForOperation:(UINavigationControllerOperation)operation
                                                fromViewController:(UIViewController *)fromVC
                                                  toViewController:(UIViewController *)toVC {
    if (operation == UINavigationControllerOperationPop) {
        return self.animation;
    }
    return nil;
}

STEP6:  Over 了

源码下载地址: http://download.csdn.net/detail/luozhonglan/7979545

***************************************************************************************************************************************************************************************

时间: 2024-10-12 08:21:58

iOS7使用代理自定义导航转场动画的相关文章

iOS7教程系列:自定义导航转场动画以及更多

感谢翻译小组成员dingdaojun热心翻译.本篇文章是我们每周推荐优秀国外的技术类文章的其中一篇.如果您有不错的原创或译文,欢迎提交给我们,更欢迎其他朋友加入我们的翻译小组(联系qq:2408167315). 在iOS7以前,开发者如果希望定制导航控制器推入推出视图时的转场动画,一般都只能通过子类化UINavigationController或者自己编写动画代码去覆盖相应的方法,现在iOS7为开发者带来了福音,苹果公司引入了大量新API,给予了开发者很高的自由度,在处理由UIViewContr

解决 ios7.0 以后自定义导航栏左边按钮靠右的问题

解决 ios7.0 以后自定义导航栏左边按钮靠右的问题 www.111cn.net 编辑:edit02_lz 来源:转载 最近开发了一个ios的app,在ios7.0+出现自定义导航栏左边按钮出现靠右的情况,后来自己解决了,解决办法如下 1.自定义按钮  代码如下 复制代码 //左按钮UIButton *leftBtn = [[UIButton alloc]initWithFrame:RectWithPara(-20, 0, 44, 44)];[leftBtn addTarget:self ac

iOS自定义segue和自定义segue转场动画

自定义segue需要继承 UIStoryboardSegue类,然后重写-(void)perform;方法.在方法中可以自定义转场动画. </pre><span style="color: rgb(97, 34, 174); font-family: Courier; font-size: 18px;">CoolSegue.h文件</span><p></p><p></p><pre code_sni

解决ios7.0 以后自定义导航栏左边按钮靠右的问题

1.自定义按钮 //左按钮 UIButton *leftBtn = [[UIButton alloc]initWithFrame:RectWithPara(-20, 0, 44, 44)]; [leftBtn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];//设置按钮点击事件 [leftBtn setBackgroundImage:ImageNamed(@"ic

iOS 自定义push转场动画

在需要使用的控制器中添加#import "BSYAnimator.h"和#import "BSYTransitionAnimator.h"两个头文件: @property(nonatomic,strong)BSYAnimator Animator;@property(nonatomic,strong)BSYTransitionAnimator *TransitionAnimator;///遵循代理@interface ViewController ()///需要跳转

关于自定义转场动画,我都告诉你。

原文出处: 伯恩的遗产(@翁呀伟呀 ) 概述 这篇文章,我将讲述几种转场动画的自定义方式,并且每种方式附上一个示例,毕竟代码才是我们的语言,这样比较容易上手.其中主要有以下三种自定义方法,供大家参考: Push & Pop Modal Segue 前两种大家都很熟悉,第三种是 Stroyboard 中的拖线,属于 UIStoryboardSegue 类,通过继承这个类来自定义转场过程动画. Push & Pop 首先说一下 Push & Pop 这种转场的自定义,操作步骤如下: 创

关于自定义转场动画,我都告诉你

作者:@翁呀伟呀 概述 这篇文章,我将讲述几种转场动画的自定义方式,并且每种方式附上一个示例,毕竟代码才是我们的语言,这样比较容易上手.其中主要有以下三种自定义方法,供大家参考: Push & Pop Modal Segue 前两种大家都很熟悉,第三种是 Stroyboard 中的拖线,属于 UIStoryboardSegue 类,通过继承这个类来自定义转场过程动画. Push & Pop 首先说一下 Push & Pop 这种转场的自定义,操作步骤如下: 1. 创建一个文件继承自

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

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

自定义转场动画--Swift3.0版本

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